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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6693785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:56:09  来源:igfitidea点击:

How to format numbers with no grouping separator

java

提问by Perlos

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( '
 DecimalFormat dec = new DecimalFormat();     
 dec.setGroupingUsed(false);
' ); 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

我想将“1000”作为没有其他字符的字符串。请帮忙

note(react to post): I want to formate the number only, without grouping.

注意(对帖子做出反应):我只想对数字进行组合,而不进行分组。

回答by powerMicha

Simply:

简单地:

import java.math.*;

public class TestBigDecimal
{
    public static void main(String[] args)
    {
        BigDecimal number = new BigDecimal(1000);
        String result = number.toString();
    }
}

回答by Hunter McMillen

If you dont want any formatting marks in the number you should just call toString on the BigDecimal Object.

如果您不希望数字中有任何格式标记,您应该只在 BigDecimal 对象上调用 toString。

String result = String.valueOf(number);

回答by Jacob

##代码##

returns your number as a string with no formatting.

将您的号码作为没有格式的字符串返回。