How to print formatted BigDecimal values?
public static String currencyFormat(BigDecimal n) {
return NumberFormat.getCurrencyInstance().format(n);
}
It will use your JVM’s current default Locale
to choose your currency symbol. Or you can specify a Locale
.
NumberFormat.getInstance(Locale.US)
For more info, see NumberFormat
class.
Converting a BigDecimal Number to the format of ###.#
Use new DecimalFormat("000.0")
Examples
DecimalFormat df = new DecimalFormat("000.0", DecimalFormatSymbols.getInstance(Locale.US));
System.out.println(df.format(new BigDecimal("1"))); // prints: 001.0
System.out.println(df.format(new BigDecimal("11.1"))); // prints: 011.1
System.out.println(df.format(new BigDecimal("123.456"))); // prints: 123.5
System.out.println(df.format(new BigDecimal(".07"))); // prints: 000.1
System.out.println(df.format(new BigDecimal("123456"))); // prints: 123456.0
Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part
I used DecimalFormat
for formatting the BigDecimal
instead of formatting the String
, seems no problems with it.
The code is something like this:
bd = bd.setScale(2, BigDecimal.ROUND_DOWN);
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
String result = df.format(bd);
How can I specify the number of decimal places of a BigDecimal to print?
Use DecimalFormat
:
String formatted = new DecimalFormat("#.##").format(val);
Is there a way to assign a formatted BigDecimal number to a BigDecimal?
Numbers (no matter what kind of - BigDecima, Float,Long) are to store values (most accurate ones) not their String representation. You cannot "store formatted value" as number and expect it to be represented that way all the time. Everytime you want to have formatted value, you have to format it.
so here
BigDecimal a = new BigDecimal(format(d, 2));
you are just creating new BigDecimal using some formatted number which is a string. That string is parsed back to the number and thats it.
What you expect to happen is similar to saying that you want "string" to be stored "as is" in binary system - as this is how numbers are stored physically. Nope, it will still be bunch of ones and zeroes.
Format BigDecimal number with commas upto 2 decimal places
You can use NumberFormat
for what you want. Here is the function I use:
public static String GenerateFormat(Double value) {
if (value == null) return "";
NumberFormat nf = NumberFormat.getInstance(new Locale("de", "DE"));
nf.setMinimumFractionDigits(0);
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(true);
return nf.format(value);
}
In this, you can see that I am providing Locale as a parameter for NumberFormat. Each country has its own number formatting standard, and what you want can be achieved with this locale new Locale("en", "US")
. Inside setMaximumFractionDigits
you can place how much of fraction digits you want, in your case it is 2.
EDIT
Because of your situation, try this:
public static String GenerateFormat(Double value) {
if (value == null) return "";
NumberFormat nf = NumberFormat.getInstance(new Locale("de", "DE"));
if (value%1 == 0) nf.setMinimumFractionDigits(0);
else nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(true);
return nf.format(value);
}
Format BigDecimal without scientific notation with full precision
To preserve the precision for a BigDecimal
you need to pass the value in as a String
BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901");
System.out.println(d.toPlainString());
Related Topics
Java Inetaddress.Getlocalhost(); Returns 127.0.0.1 ... How to Get Real Ip
Highlighting Strings in Javafx Textarea
How to Fetch Fetchtype.Lazy Associations with JPA and Hibernate in a Spring Controller
Coding Conventions - Naming Enums
Instanceof - Incompatible Conditional Operand Types
Docker: Combine Multiple Images
Primitive Type 'Short' - Casting in Java
Regex Date Format Validation on Java
Javafx Location Is Not Set Error Message
Migration from Struts 1 to Struts 2
Ignoring Ssl Certificate in Apache Httpclient 4.3
Differencebetween 'Java', 'Javaw', and 'Javaws'
What's Up with Java's "%N" in Printf