Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

truncating java doubles

Thread view: 
Jeremy Watts - 23 Oct 2007 19:10 GMT
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);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.