I have a program which does some stuff for a payroll (this is only an exercise for me).
Well, I'm using doubles as my variable Type. However, whenever i'm displaying the money, it is showing the actual pay, which is showing 3 decimal places. How can I eliminate all of the decimals past the 2nd?
Thanks! Please remember I'm a beginner!
We have the String.format method and the Formatter class in j2se 1.5+:
Before j2se 1.5 we had the NumberFormat and DecimalFormat classes.
double d = 1.0/3.0;
System.out.println("d = " + d);
String s = String.format("%.2f", d);
System.out.println("s = " + s);
// Another way.
System.out.printf("formatted d = %.2f%n", d);
// Trick not recommended for $
double f = ((int)(d * 100))/100;
System.out.println("f = " + f);
do u know C language?
in c we can use printf() function that satisfy your requirement.in the same manner we can use system.out.printf() in java.
before use this function u had to revisit C printf () function