Question

I have a bea weblogic 8.1 sp5 server running on a JRockit instance with the following java version:

*java -version java version "1.4.2_10" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_10-b03) BEA JRockit(R) (build R26.3.0-32-58710-1.4.2_10-20060308-2022-linux-ia64, )*

I face an issue when the following code is executed:

int debt = 27003; //amount in cents; System.out.println(((double) debt18 / 100)); //amount in euro

The printed result is 270.03000000000003

I am sure that this is a virtual machine error since it is observable on this particular jrockit machine.

Do you happen to face a similar problem in the past?

Can you please tell me a less error-prone way to pass the wanted value to a double variable,preferably using standard java libraries?

Was it helpful?

Solution

I know that this is pretty old but i have an answer and maybe it will help somebody in the future. I used the DecimalFormat object to keep only the digits that were useful to me. Only one decimal was needed and it was always correct, even when the error appeared so this is OK for me.

The code is the following:

String pattern = "##0.0#";
DecimalFormatSymbols separator = new DecimalFormatSymbols();
separator.setDecimalSeparator('.');
DecimalFormat decimalFormatter = new DecimalFormat(pattern, separator);

int debt = 27003; //amount in cents;
System.out.println(decimalFormatter.format(((double) debt18 / 100)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top