fixthebug2003
Programmer
HI,
Does anybody know the function to round off a numeric value to 2 decimal digits..
fixthebug2003
Does anybody know the function to round off a numeric value to 2 decimal digits..
fixthebug2003
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
import java.text.DecimalFormat;
import java.text.NumberFormat;
class ro
{
public static void main(String args[])
{
double input = 12.056;
double input2 = 43.4;
String outputString1, outputString2;
String pattern = "########0.00";
DecimalFormat outputBase = new DecimalFormat();
outputBase.applyPattern(pattern);
System.out.println("Result "+String.valueOf(outputBase.format(input2))); // String result has 2 d.p.
System.out.println("ResultB "+Double.parseDouble(outputBase.format(input2))); // double result seems to have 1 d.p.
// the following is another method for outputting the value with 2 d.p., the results may have 1 d.p.
double output, output2;
output = Math.round(input*100)/100d; // 100d is a must for a double result here
System.out.println("output "+output);
output2 = Math.round(input2*100)/100d; // 100d is a must for a double result here
System.out.println("output2 "+output2);
}
}