PDA

View Full Version : [SOLVED] What does # do in DecimalFormat in Java?



s3a
October 18th, 2009, 04:43 AM
In the following code:

import java.text.DecimalFormat;

import java.util.Scanner;

public class decform_checker
{
public static void main (String [] args)
{
Scanner kb = new Scanner(System.in);

double number = kb.nextDouble();

DecimalFormat formatter = new DecimalFormat("#0000.000");
System.out.println(formatter.format(number));
}
}

What does the # do in the line in bold?

Any input would be greatly appreciated!
Thanks in advance!

kavon89
October 18th, 2009, 05:26 AM
Try the Java API, once you learn to read/browse it you'll know how everything in the Java library works. ;D

http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html

SSTwinrova
October 18th, 2009, 05:26 AM
http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html

Scroll down to Special Format Characters, it has what all the various possibilities mean.

s3a
October 20th, 2009, 01:02 AM
All it says is

"# Number Yes Digit, zero shows as absent ."

That is not enough information for me to understand :(.

myrtle1908
October 20th, 2009, 08:21 AM
Both '#' and '0' mean digit. If you use '#' but you do not supply a digit then it (the result) is left empty (blank). If you use '0' but do not supply a digit in said position then zero is inserted instead.

Take this example format: "####.000"

This pattern means four places before the decimal point, which are empty if not filled, and three places after the decimal point, which are 0 if not filled.

So if you supplied '12.65' to this pattern then the result would be '12.650'.

s3a
October 20th, 2009, 01:46 PM
Thanks!