...

Decimals in Java

ragol_67
09-30-2004, 09:51 PM
How do you assign a certain number of decimal points in a java program?

When I print out a message using System.out.println, the system prints out numbers with about 10 decimal places.

Thanks,
Nick!

Jason
09-30-2004, 10:01 PM
import java.text.NumberFormat;
..................
NumberFormat nf = NumberFormat.getNumberInstance() ;
nf.setGroupingUsed(false) ; // don't group by threes
nf.setMaximumFractionDigits(2) ;
nf.setMinimumFractionDigits(2) ;
System.out.println("float unformatted " + d1 + " now formatted " + nf.format(d1));




Jason

Celtboy
10-01-2004, 07:34 AM
it also depends on what type your variables are. Floats, doubles, ints...

Otherwise the numberformat class is the way to go.

ragol_67
11-16-2004, 11:45 PM
My variables are declared as doubles.

Could someone please give me an example of how to implement the above code?


Thanks,
Nick!

Brandoe85
11-17-2004, 12:41 AM
That code is all you need:
import java.text.NumberFormat;
public class decimals

{
public static void main(String[] args)

{
NumberFormat nf = NumberFormat.getNumberInstance() ;
nf.setGroupingUsed(false) ; // don't group by threes
nf.setMaximumFractionDigits(2) ;
nf.setMinimumFractionDigits(2) ;
double d1 = 3.427223;
System.out.println("float unformatted " + d1 + " now formatted " + nf.format(d1));

}

}

ragol_67
11-17-2004, 01:39 AM
Ah, okay! I've got it.

Thank You!!

:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum