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/