How to output a double number

P

premmehrotra

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.
 
M

Mark Thomas

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.
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
 
M

Mark Thomas

Mark said:
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
Whoops - that should be DecimalFormat.
 
R

Rhino

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.
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
 
P

premmehrotra

Thanks a lot to all. This is exactly what I was looking for. I am using
Java 1.4.x
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top