简体   繁体   中英

How to format numbers with no grouping separator

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.

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();
    }
}
String result = String.valueOf(number);

将您的数字作为字符串返回,没有格式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM