I'm trying to format a BigDecimal value by using methods of DecimalFormat.format().
My problem, is I don't know how to set DecimalFormats DecimalFormatSymbol to format text without any grouping separators.
I'd like to know how to set a grouping symbol and use the format methods. I know how to do it differently by using replace or others methods but it's not what I want.
So I need to know how to set an empty character as grouping operator.
Example:
DecimalFormat dec = new DecimalFormat(); DecimalFormatSymbols decFS = new DecimalFormatSymbols(); decFS.setGroupingSeparator( '\0' ); dec.setDecimalFormatSymbols( decFS ); BigDecimal number = new BigDecimal(1000); String result = dec.format(number);
I want to get "1000" as string with no other characters. Please help
note(react to post): I want to formate the number only, without grouping.
In many programming languages, the thousands separator (e.g., the "," in the American string "1,000") is called the "grouping separator". Why is this? Are there any real-world locales that separate written integers on some other boundary? Do people somewhere write numbers like 86,75,30,9 or 8675,309?
You can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others.
NumberFormat is a Java class for formatting and parsing numbers. With NumberFormat , we can format and parse numbers for any locale. NumberFormat allows us to round values, set decimal separators, set the number of fraction digits, or format values according to a specific locale.
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.
Simply:
DecimalFormat dec = new DecimalFormat(); dec.setGroupingUsed(false);
If you dont want any formatting marks in the number you should just call toString on the BigDecimal Object.
import java.math.*; public class TestBigDecimal { public static void main(String[] args) { BigDecimal number = new BigDecimal(1000); String result = number.toString(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With