decimalformat

P

(-Peter-)

Hi..

Can anybody help me how to print some output in a beautiful way?

I want to have the decimal format 0,### ALWAYS..

That is, if I for example have a double = 0,1 I want this to be
printed as 0,100, and not 0,1..

How do I force java to print the zeros?

/Peter
 
E

Eric Sosman

(-Peter-) said:
Hi..

Can anybody help me how to print some output in a beautiful way?

I want to have the decimal format 0,### ALWAYS..

That is, if I for example have a double = 0,1 I want this to be
printed as 0,100, and not 0,1..

NumberFormat fmt = new DecimalFormat("0.000");
System.out.println(fmt.format(0.1));

or

NumberFormat fmt = NumberFormat.getInstance();
fmt.setMinimumFractionDigits(3);
fmt.setMaximumFractionDigits(3);
System.out.println(fmt.format(0.1));
 
P

(-Peter-)

NumberFormat fmt = new DecimalFormat("0.000");
System.out.println(fmt.format(0.1));

or

NumberFormat fmt = NumberFormat.getInstance();
fmt.setMinimumFractionDigits(3);
fmt.setMaximumFractionDigits(3);
System.out.println(fmt.format(0.1));

Thank you very much :)

/Peter
 
S

Stefan Ram

(-Peter-) said:
0,1 I want this to be printed as 0,100, and not 0,1..

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.printf( "%.03f%n", 0.1 ); }}

0,100
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top