Any way to decide how many decimals to show in equation?

B

Bengan

Hi,

Is there any good way to decide how many decimals you want to show in a equation.

I.e i (double)10 / (double)11 = 0.90909090....... but i want to round the
result and show 0.91.

Best regards
/Bengan
 
G

Gordon Beaton

Hi,

Is there any good way to decide how many decimals you want to show in a equation.

I.e i (double)10 / (double)11 = 0.90909090....... but i want to round the
result and show 0.91.

java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");

double num = ...
System.out.println(df.format(num));


/gordon
 
J

Jon A. Cruz

Gordon said:
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");

double num = ...
System.out.println(df.format(num));


Or (depending on the needs of the program) you can use the 'superset'
formatting class:

String str = java.text.MessageFormat.format( "The value is
{0,number,#.##}", new Object[] {new Double(num)} );

System.out.println( str );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top