PDA

Click to See Complete Forum and Search --> : control decimal places


System_Error
Jan 11th, 2005, 06:00 AM
Is there a way that I can control the number of decimal places a number has?

For instance if a calculation = 5.42343423456453454

can I make this 5.42?

Dillinger4
Jan 11th, 2005, 12:26 PM
Look into the java.text.DecimalFormat class. There is a method that allows you to set the maximum amount of decimal places. Im pretty sure you can also apply a pattern using the DecimalFormat class.

Dillinger4
Jan 11th, 2005, 12:38 PM
Here's some code to give you an idea.

NumberFormat nf = NumberFormat.getNumberInstance();
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("###.##");
double calculation = 5.42343423456453454;
System.out.println(calculation);
System.out.println(df.format(calculation));

System_Error
Jan 11th, 2005, 02:48 PM
Thank you very much.