简体   繁体   English

如何格式化没有分组分隔符的数字

[英]How to format numbers with no grouping separator

I'm trying to format a BigDecimal value by using methods of DecimalFormat.format(). 我正在尝试使用DecimalFormat.format()方法格式化BigDecimal值。

My problem, is I don't know how to set DecimalFormats DecimalFormatSymbol to format text without any grouping separators. 我的问题是,我不知道如何设置DecimalFormats DecimalFormatSymbol来格式化文本而不使用任何分组分隔符。

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. 我希望将“1000”作为字符串而没有其他字符。 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. 如果你不想在数字中有任何格式标记,你应该在BigDecimal对象上调用toString。

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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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