如何打印一个没有逗号的双引号

时间:2021-12-09 02:25:23

When using toString(), Double adds commas (5143 is printed as 5,143). How to disable the commas?

使用toString()时,双加逗号(5143打印为5143)。如何禁用逗号?

10 个解决方案

#1


12  

Your problem belongs to Locale, as pointed out correctly by Rorick. However, you should look into DecimalFormat class, in case changing Locale means mess up all the things.

您的问题属于语言环境,罗瑞克正确地指出了这一点。但是,您应该查看DecimalFormat类,以防更改语言环境会导致混乱。

Look at NumberFormat class, to deal with thousand separator. Because it seems your case is regarding thousand separator instead.

查看NumberFormat类,处理千位分隔符。因为你的情况似乎是关于千隔板的。

#2


6  

As far as I am aware, you can not disable what the toString() method returns.

就我所知,您不能禁用toString()方法返回的内容。

My solution would be as follows:

我的解决办法如下:

someDouble.toString().replaceAll(",", "");

Not the most elegant solution, but it works.

这不是最优雅的解决方案,但它确实有效。

#3


4  

myDouble.toString().replaceAll(",", "");

#4


4  

Java has excellent support for formatting numbers in text in different locales with the NumberFormat class:

Java非常支持使用NumberFormat类在不同地区对文本中的数字进行格式化:

With current locale:

与当前语言环境:

NumberFormat.getNumberInstance().format(5000000);

will get you (with swedish locale) the string: 5 000 000

你会得到(瑞典语言环境)字符串:5000 000吗

...or with a specific locale (e.g. french, which also results in 5 000 000):

…或有特定的语言环境(例如法语,也有5000 000):

NumberFormat.getNumberInstance(Locale.FRANCE).format(5000000);

#5


4  

This will remove all grouping (in your case commas).

这将删除所有分组(在您的案例中是逗号)。

DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);

#6


3  

Probably, you have to change your locale settings. It is taken by default from system locale, but you can override this. Read javadoc on Locale class and this little tutorial to start. Locale can be specified through command line:

您可能需要更改语言环境设置。它默认来自系统语言环境,但是您可以重写它。阅读本地化类的javadoc和这个小教程。可以通过命令行指定语言环境:

java -Duser.language=en -Duser.region=US MyApplication

#7


3  

Three ways:

三种方式:

  1. Using the DecimalFormat

    使用DecimalFormat

    DecimalFormat df = new DecimalFormat();
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    dfs.setGroupingSeparator(Character.MAX_VALUE);
    df.setDecimalFormatSymbols(dfs);
    System.out.println(df.format(doubleVar));
    
  2. (as suggested by others) just replace the comma in the string that you get

    (正如其他人建议的那样)只需替换字符串中的逗号

  3. Set the locale on load of your VM
  4. 在VM的负载上设置语言环境

#8


2  

NumberFormat format = NumberFormat.getInstance();

NumberFormat格式= NumberFormat.getInstance();

format.setGroupingUsed(false);

format.setGroupingUsed(假);

#9


1  

I use this method to format a double to string with a fixed locale, no grouping and with a minimum and maximum of fraction digits

我使用这种方法来格式化具有固定语言环境、无分组、最小和最大分数位数的双到字符串

public String formatNumber(double number){
    NumberFormat nf = NumberFormat.getInstance(new Locale("en", "EN"));
    nf.setMaximumFractionDigits(3);  
    nf.setMinimumFractionDigits(1);
    nf.setGroupingUsed(false);
    String str = nf.format(number);
    return str;       
 }

#10


0  

Double result= 5143.0;

Sysout(result.toString()) 

gives me 5143.0... can u put the code for which u got so

给我5143.0……你能把你得到的代码放进去吗?

#1


12  

Your problem belongs to Locale, as pointed out correctly by Rorick. However, you should look into DecimalFormat class, in case changing Locale means mess up all the things.

您的问题属于语言环境,罗瑞克正确地指出了这一点。但是,您应该查看DecimalFormat类,以防更改语言环境会导致混乱。

Look at NumberFormat class, to deal with thousand separator. Because it seems your case is regarding thousand separator instead.

查看NumberFormat类,处理千位分隔符。因为你的情况似乎是关于千隔板的。

#2


6  

As far as I am aware, you can not disable what the toString() method returns.

就我所知,您不能禁用toString()方法返回的内容。

My solution would be as follows:

我的解决办法如下:

someDouble.toString().replaceAll(",", "");

Not the most elegant solution, but it works.

这不是最优雅的解决方案,但它确实有效。

#3


4  

myDouble.toString().replaceAll(",", "");

#4


4  

Java has excellent support for formatting numbers in text in different locales with the NumberFormat class:

Java非常支持使用NumberFormat类在不同地区对文本中的数字进行格式化:

With current locale:

与当前语言环境:

NumberFormat.getNumberInstance().format(5000000);

will get you (with swedish locale) the string: 5 000 000

你会得到(瑞典语言环境)字符串:5000 000吗

...or with a specific locale (e.g. french, which also results in 5 000 000):

…或有特定的语言环境(例如法语,也有5000 000):

NumberFormat.getNumberInstance(Locale.FRANCE).format(5000000);

#5


4  

This will remove all grouping (in your case commas).

这将删除所有分组(在您的案例中是逗号)。

DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);

#6


3  

Probably, you have to change your locale settings. It is taken by default from system locale, but you can override this. Read javadoc on Locale class and this little tutorial to start. Locale can be specified through command line:

您可能需要更改语言环境设置。它默认来自系统语言环境,但是您可以重写它。阅读本地化类的javadoc和这个小教程。可以通过命令行指定语言环境:

java -Duser.language=en -Duser.region=US MyApplication

#7


3  

Three ways:

三种方式:

  1. Using the DecimalFormat

    使用DecimalFormat

    DecimalFormat df = new DecimalFormat();
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    dfs.setGroupingSeparator(Character.MAX_VALUE);
    df.setDecimalFormatSymbols(dfs);
    System.out.println(df.format(doubleVar));
    
  2. (as suggested by others) just replace the comma in the string that you get

    (正如其他人建议的那样)只需替换字符串中的逗号

  3. Set the locale on load of your VM
  4. 在VM的负载上设置语言环境

#8


2  

NumberFormat format = NumberFormat.getInstance();

NumberFormat格式= NumberFormat.getInstance();

format.setGroupingUsed(false);

format.setGroupingUsed(假);

#9


1  

I use this method to format a double to string with a fixed locale, no grouping and with a minimum and maximum of fraction digits

我使用这种方法来格式化具有固定语言环境、无分组、最小和最大分数位数的双到字符串

public String formatNumber(double number){
    NumberFormat nf = NumberFormat.getInstance(new Locale("en", "EN"));
    nf.setMaximumFractionDigits(3);  
    nf.setMinimumFractionDigits(1);
    nf.setGroupingUsed(false);
    String str = nf.format(number);
    return str;       
 }

#10


0  

Double result= 5143.0;

Sysout(result.toString()) 

gives me 5143.0... can u put the code for which u got so

给我5143.0……你能把你得到的代码放进去吗?