Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Number Format

  Asked By: Thelma    Date: Oct 04    Category: Java    Views: 753
  

Does anyone know how to format doubles to only print out one place
after the decimal?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Bathilda Schmidt     Answered On: Oct 04

Use java.text.NumberFormat.

You'll want to do something like:

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(1);

and then do nf.format(yourdouble)

 
Answer #2    Answered By: Joyce Edwards     Answered On: Oct 04

You can also use:
double d = 12.345;
DecimalFormat df = new DecimalFormat("0.#");
df.format(d);

 
Answer #3    Answered By: Adel Fischer     Answered On: Oct 04

You could use the java.text.DecimalFormat class

Documentation attached, sample code found below..

/**
*
* formats a double
*
* @author Kristoffer Hell
*
*/

import java.text.*;

public class DoubleFormatExample {

public static void main (String args[]) {

double dbl = 21345.099876;
System.out.println("as double: " + dbl);
DecimalFormat df = new
DecimalFormat("##########.0");
String fDec = df.format(dbl);
System.out.println(fDec);

}

}

 
Didn't find what you were looking for? Find more on Number Format Or get search suggestion and latest updates.




Tagged: