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 / April 2006

Tip: Looking for answers? Try searching our database.

How to output a double number

Thread view: 
premmehrotra@hotmail.com - 26 Apr 2006 20:22 GMT
have a varaiable of type double. When I output using system.out.print,
I want to do following:

1. Output should not be in Exponent format, i.e., 1000000000E07
2. I want only a fixed no of  decimal numbers i.e., 23.456   27.555
29.052

How can I do above two.
Mark Thomas - 26 Apr 2006 20:28 GMT
>  have a varaiable of type double. When I output using system.out.print,
> I want to do following:
[quoted text clipped - 4 lines]
>
> How can I do above two.

At Java 5.0 you can use printf instead of print.  Otherwise, have a look
at the java.text package, in particular the NumberFormat class.

Mark
Mark Thomas - 26 Apr 2006 20:31 GMT
>>  have a varaiable of type double. When I output using system.out.print,
>> I want to do following:
[quoted text clipped - 9 lines]
>
> Mark
Whoops - that should be DecimalFormat.
Rhino - 26 Apr 2006 20:39 GMT
> have a varaiable of type double. When I output using system.out.print,
> I want to do following:
[quoted text clipped - 4 lines]
>
> How can I do above two.

Use a NumberFormat.

For example:

Double myDouble = 1.5671097486754;
NumberFormat numberFormat =
NumberFormat.getNumberInstance(Locale.getDefault());
numberFormat.setMaximumFractionDigits(2);
System.out.println("Formatted number: " + numberFormat.format(myDouble));
//should write 1.57

--
Rhino
Oliver Wong - 26 Apr 2006 21:25 GMT
> have a varaiable of type double. When I output using system.out.print,
> I want to do following:
[quoted text clipped - 4 lines]
>
> How can I do above two.

   See http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

   - Oliver
premmehrotra@hotmail.com - 27 Apr 2006 02:02 GMT
Thanks a lot to all. This is exactly what I was looking for. I am using
Java 1.4.x
Tony Morris - 27 Apr 2006 04:47 GMT
> have a varaiable of type double. When I output using system.out.print,
> I want to do following:
[quoted text clipped - 4 lines]
>
> How can I do above two.

Like this: http://jqa.tmorris.net/GetQAndA.action?qids=46&showAnswers=true

Signature

Tony Morris
http://tmorris.net/



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



©2009 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.