truncating java doubles  
Author Message
Jeremy Watts





PostPosted: 2007-10-24 2:11:00 Top

java-programmer, truncating java doubles 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





PostPosted: 2007-10-24 2:28:00 Top

java-programmer >> truncating java doubles On Oct 23, 11:10 am, "Jeremy Watts" <email***@***.com> wrote:
> 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

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





PostPosted: 2007-10-24 3:32:00 Top

java-programmer >> truncating java doubles On Tue, 23 Oct 2007 18:10:54 GMT, "Jeremy Watts"
<email***@***.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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
sengsational





PostPosted: 2007-10-24 3:37:00 Top

java-programmer >> truncating java doubles On Oct 23, 2:10 pm, "Jeremy Watts" <email***@***.com> wrote:
> 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

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





PostPosted: 2007-10-24 21:27:00 Top

java-programmer >> truncating java doubles sengsational wrote:


>
> 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);