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?
Printable View
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?
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.
Here's some code to give you an idea.
Code:NumberFormat nf = NumberFormat.getNumberInstance();
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("###.##");
double calculation = 5.42343423456453454;
System.out.println(calculation);
System.out.println(df.format(calculation));
Thank you very much.