My title of this question I have isn't so great, but hopefully I can explain it more in this post.
import java.io.*;
import java.text.*;
public class Output {
public static void main(String[] args) {
/*double number = 438.978;
/*UpperCase <- naming convention for classes.DecimalFormat x = new DecimalFormat("#.#");
System.out.println(x.format(number));*/
double number = 43.97;
DecimalFormat x = new DecimalFormat(".###");
System.out.println(x.format(number));
}
}
Don't mind the comments. During my Gr 11 Comp Sci class I asked my teacher if the hashtags on the left of the decimal point(11th line) did anything to the double number, we tried it as a class and we found that it did not change the output of the System.out.println statement.
~/workspace/Java/ $ java Output
43.97
Can someone explain to me the purpose of the parameters to the left of the decimal? Someone programmed it to do something so I was just curious.
Like others have said #
formats a digit but drops zeros.
In your example where you apply .###
to a double like 12.34
it would format to 12.340
but since it drops zeros it does not.
The same happens when you put a #
before the decimal, for example ###.###
to format 12.34
would display 012.340
but since it drops zeros it displays as 12.34
.
So a #
before the decimal will really do nothing.
An example of something useful before the decimal, so you can see formats before the decimal can work is 0
which formats a digit but does not drop zeros, and can also be used in the DecimalFormat. A pattern like 000.000
applied to 12.34
results in 012.340
:
double d = 12.34;
DecimalFormat df = new DecimalFormat("000.000");
System.out.print(df.format(d));
Patterns like 0
, #
and more are defined in DecimalFormat.