Yes i know I will read the article but as I said just wanted get this working before i go home today.
This is how I fixed it anyway
DecimalFormat threeDecimals = new DecimalFormat("0.000");
thirdOfWk = Double.parseDouble(threeDecimals.format(thirdOfWk));
Thanks for the help guys
> Yes i know I will read the article but as I said just
> wanted get this working before i go home today.
>
> This is how I fixed it anyway
>
> DecimalFormat threeDecimals = new DecimalFormat("0.000");
> thirdOfWk = Double.parseDouble(threeDecimals.format(thirdOfWk));
That doesn't really resolve the problem. What does the following output now?
System.out.println(thirdOfWk);
I doubt it will be what you want (and even if it is, this isn't a reliable method for getting the printed format that you desire).
> Yes i know I will read the article but as I said just
> wanted get this working before i go home today.
>
> This is how I fixed it anyway
>
> DecimalFormat threeDecimals = new
> DecimalFormat("0.000");
> thirdOfWk =
> Double.parseDouble(threeDecimals.format(thirdOfWk));
>
> Thanks for the help guys
I think you are going to need to do some rounding otherwise this will not fix all the problems.
Consider that:
111.20000000000002
truncated to 3 decimals is:
111.200
But what if the number comes up something like this (I am not sure that it can happen but I would guess that it can):
123.19999999999998 (instead of 123.2)
truncated to 3 decimals is:
123.199
And I think doremifasollatido is correct and you have another problem (because you are putting the truncated number back into a Double).
You really need to use BigDecimal or integers. For integers you would need to scale the numbers and then display with decimals.
Message was edited by:
jbish
> I think you are going to need to do some rounding
> otherwise this will not fix all the problems.
>
> Consider that:
> 111.20000000000002
> truncated to 3 decimals is:
> 111.200
>
> But what if the number comes up something like this
> (I am not sure that it can happen but I would guess
> that it can):
> 123.19999999999998 (instead of 123.2)
> truncated to 3 decimals is:
> 123.199
>
Happily NumberFormat with a limited number of fraction digits set rounds, rather than trucates. An the moral is - always use DecimalFormat/NumberFormat when outputing float values as text except when it's debugging and dosen't matter.
And there's really not much point in rounding a floating point value except when converting to an integer or string representation, because the errors may always come back. Particularly since the internal representation is binary, not decimal.
> Happily NumberFormat with a limited number of
> fraction digits set rounds, rather than trucates. An
> the moral is - always use DecimalFormat/NumberFormat
> when outputing float values as text except when it's
> debugging and dosen't matter.
Thanks for the clarification. I have to admit I was too lazy to look at the API even though the thought had crossed my mind.