There is a slight difference between these two formats. The "#.##" means it will print the number with maximum two decimal places whereas "#.00" means it will always display two decimal places and if the decimal places are less than two, it will replace them with zeros. see the example below with output.
public static final DecimalFormat df1 = new DecimalFormat( "#.##" );
public static final DecimalFormat df2 = new DecimalFormat( "#.00" );
System.out.println(df1.format(7.80));
System.out.println(df1.format(85));
System.out.println(df1.format(85.786));
System.out.println(df2.format(7.80));
System.out.println(df2.format(85));
System.out.println(df2.format(85.786));
And the output will be
7.8
85
85.79
7.80
85.00
85.79
Answer from Raza on Stack OverflowDecimal Format
This is not the answer your professor is looking for... but please don't ever use double or float for monetary units. Things can go so wrong its not even funny... and the amount of time you spend looking for off by a penny errors will make you question your sanity.
The right way to do this would be to use BigDecimal on the value "12500" and then adjust the number to go from cents to dollars.
import java.math.BigDecimal;
public class BigD {
public static void main (String[] args) {
BigDecimal cents = new BigDecimal("12500");
System.out.println(cents.toString());
BigDecimal dollars = cents.movePointLeft(2);
System.out.println(dollars.toString());
}
}(ideone)
Give Why not use Double or Float to represent currency? and When do rounding problems become a real problem? Is the least significant digit being one off really a big deal? a read. How to calculate monetary values in Java has another nice simple that shows some errors in floating point.
Even though you may be able to print the value out correctly, it doesn't mean that it is represented correctly.
12500 and 125 happen to be nice numbers in there... but 125.01 is actually 125.010000000000005115907697473
More on reddit.com[Java] DecimalFormat vs NumberFormat?
Double decimal formatting in Java - Stack Overflow
[JAVA] Cannot convert from double to decimal format.
DecimalFormat is not a numerical type. It does not do calculations or hold numbers. It is a type designed to help PRINT OUT a numerical type.
double taxTotal;
DecimalFormat formatter = new DecimalFormat ("$###,###.##");
// blah blah
string formattedValue = formatter.format(taxTotal)
System.out.print("Your 2012 tax is " + formattedValue );
Note: You shouldn't use floating point types (i.e. float or duuble) to store money
-
http://www.javapractices.com/topic/TopicAction.do?Id=13
-
http://stackoverflow.com/questions/285680/representing-monetary-values-in-java
Videos
There is a slight difference between these two formats. The "#.##" means it will print the number with maximum two decimal places whereas "#.00" means it will always display two decimal places and if the decimal places are less than two, it will replace them with zeros. see the example below with output.
public static final DecimalFormat df1 = new DecimalFormat( "#.##" );
public static final DecimalFormat df2 = new DecimalFormat( "#.00" );
System.out.println(df1.format(7.80));
System.out.println(df1.format(85));
System.out.println(df1.format(85.786));
System.out.println(df2.format(7.80));
System.out.println(df2.format(85));
System.out.println(df2.format(85.786));
And the output will be
7.8
85
85.79
7.80
85.00
85.79
This doesn't seem to be solved by a single formatter. I suggest you use "0.00" format and replace ".00" with an empty string.
public static String myFormat(double number) {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(number).replaceAll("\\.00$", "");
}
Im trying to turn a string number of "12500" into the formatted version of "$125.00", but whenever i use the Decimal format it wont put the Decimal between the "125" and "00" after using the format ("$###.##"). Im assuming its a simple fix, but im just stuck.. Any help?
This is not the answer your professor is looking for... but please don't ever use double or float for monetary units. Things can go so wrong its not even funny... and the amount of time you spend looking for off by a penny errors will make you question your sanity.
The right way to do this would be to use BigDecimal on the value "12500" and then adjust the number to go from cents to dollars.
import java.math.BigDecimal;
public class BigD {
public static void main (String[] args) {
BigDecimal cents = new BigDecimal("12500");
System.out.println(cents.toString());
BigDecimal dollars = cents.movePointLeft(2);
System.out.println(dollars.toString());
}
}
(ideone)
Give Why not use Double or Float to represent currency? and When do rounding problems become a real problem? Is the least significant digit being one off really a big deal? a read. How to calculate monetary values in Java has another nice simple that shows some errors in floating point.
Even though you may be able to print the value out correctly, it doesn't mean that it is represented correctly.
12500 and 125 happen to be nice numbers in there... but 125.01 is actually 125.010000000000005115907697473
Are you saying you want a String value "12500" to be formatted as "$125.00"? I'm not sure what you've tried exactly, but I think you'd want to convert the string a numeric data type (e.g. double, int, etc.), divide by 100, and format that result. Or, depending on your situation, you might be able to avoid converting to a numeric type, and just do some simple string manipulation instead.
I find the area of an object and store it in a type double. If I want a specific number of decimal places, say 5, which one is better to use?
e.g. 4.55 > 4.55000,
But I guess maybe I need to understand the idea of decimal places. If a number is 4.555555, rounded to 5 decimal places, will it be 4.55556? If so, will the DecimalFormat be what I need to look into? I see if I do #.##### it will round down, but if I use zeroes, it will round normally like in math: #.00000.
If this is correct..can someone tell me the difference between number format and decimal format then? When to use each.
One of the way would be using NumberFormat.
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(4.0));
Output:
4.00
With Java 8, you can use format method..: -
System.out.format("%.2f", 4.0); // OR
System.out.printf("%.2f", 4.0);
fis used forfloatingpoint value..2after decimal denotes, number of decimal places after.
For most Java versions, you can use DecimalFormat: -
DecimalFormat formatter = new DecimalFormat("#0.00");
double d = 4.0;
System.out.println(formatter.format(d));