Hi,
How do you truncate java doubles? I am trying to convert a double to a
number thats rounded to 3 decimal places - i can do this using BigDecimals
easily but cant seem to find anything to do the same with a double.
Thanks
Manish Pandit - 23 Oct 2007 19:28 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>
> Thanks
NumberFormat might be the solution what you're looking for (http://
java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html).
-cheers,
Manish
Roedy Green - 23 Oct 2007 20:32 GMT
On Tue, 23 Oct 2007 18:10:54 GMT, "Jeremy Watts"
<jwatts1970@hotmail.com> wrote, quoted or indirectly quoted someone
who said :
>How do you truncate java doubles? I am trying to convert a double to a
>number thats rounded to 3 decimal places - i can do this using BigDecimals
>easily but cant seem to find anything to do the same with a double.
see http://mindprod.com/jgloss/round.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
sengsational - 23 Oct 2007 20:37 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>
> Thanks
double d = Math.PI * 20000;
NumberFormat f = NumberFormat.getInstance();
f.setMinimumFractionDigits(3);
System.out.println("d:" + d);
System.out.println("a:" + f.format(d));
System.out.println("b:" + ((int)(d * 1000))/ 1000d);
www - 24 Oct 2007 14:26 GMT
> double d = Math.PI * 20000;
> NumberFormat f = NumberFormat.getInstance();
> f.setMinimumFractionDigits(3);
> System.out.println("d:" + d);
> System.out.println("a:" + f.format(d));
> System.out.println("b:" + ((int)(d * 1000))/ 1000d);
You will see more difference(due to rounding) between a: and b:
double d = Math.PI * 20000 + 0.0006;
NumberFormat f = NumberFormat.getInstance();
f.setMinimumFractionDigits(3);
System.out.println("d:" + d);
System.out.println("a:" + f.format(d));
System.out.println("b:" + ((int)(d * 1000))/ 1000d);