I have been sitting on this for a day now thinking like a 5th grade school student.
public class Tester
{
static String actualValue = "";
private static DecimalFormat df2 = new DecimalFormat("#.##"); //To round off to two decimal places.
static double regPrice = 0.0;
static double regPrice2 = 0.0;
public static void main(String[] args)
{
regPrice2 = 1506.365;
System.out.println("reg price 1 is: "+regPrice2);
System.out.println("reg price 1 after rounding is is: "+round(regPrice2));
regPrice = 8535.765;
System.out.println("reg price is: "+regPrice);
System.out.println("reg price after rounding is: "+round(regPrice));
}
public static double round(double value)
{
df2.setRoundingMode(RoundingMode.HALF_UP);
String returnValue = df2.format(value);
double actualValue = Double.parseDouble(returnValue);
return actualValue;
}
}
Output | Value 1 | Value 2 |
---|---|---|
Actual | 1506.37 | 8535.76 |
Expected | 1506.37 | 8535.77 |
Why is the rounding off working for the first number but not the second? How can I make this work?