Java 如何在没有逗号的情况下打印 Double
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2242878/
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
how to print a Double without commas
提问by Erik Sapir
When using toString()
, Double adds commas (5143 is printed as 5,143).
How to disable the commas?
使用时toString()
,Double 添加逗号(5143 打印为 5,143)。如何禁用逗号?
采纳答案by Adeel Ansari
Your problem belongs to Locale, as pointed out correctly by Rorick. However, you should look into DecimalFormatclass, in case changing Locale means mess up all the things.
正如 Rorick 正确指出的那样,您的问题属于语言环境。但是,您应该查看DecimalFormat类,以防更改 Locale 意味着搞砸了所有事情。
Look at NumberFormatclass, to deal with thousand separator. Because it seems your case is regarding thousand separator instead.
看NumberFormat类,处理千位分隔符。因为您的情况似乎与千位分隔符有关。
回答by Kai Huppmann
myDouble.toString().replaceAll(",", "");
回答by Abhiram
Double result= 5143.0;
Sysout(result.toString())
gives me 5143.0...
can u put the code for which u got so
给我5143.0...
你能把你得到的代码吗
回答by adimitri
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.
不是最优雅的解决方案,但它有效。
回答by Rorick
Probably, you have to change your locale settings. It is taken by default from system locale, but you can override this. Read javadoc on Localeclass and this little tutorialto start. Locale can be specified through command line:
可能,您必须更改区域设置。默认情况下,它是从系统语言环境中获取的,但您可以覆盖它。阅读关于Locale类的javadoc和这个小教程开始。可以通过命令行指定语言环境:
java -Duser.language=en -Duser.region=US MyApplication
回答by Bozho
Three ways:
三种方式:
Using the
DecimalFormat
DecimalFormat df = new DecimalFormat(); DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); dfs.setGroupingSeparator(Character.MAX_VALUE); df.setDecimalFormatSymbols(dfs); System.out.println(df.format(doubleVar));
(as suggested by others) just replace the comma in the string that you get
- Set the locale on load of your VM
使用
DecimalFormat
DecimalFormat df = new DecimalFormat(); DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); dfs.setGroupingSeparator(Character.MAX_VALUE); df.setDecimalFormatSymbols(dfs); System.out.println(df.format(doubleVar));
(正如其他人所建议的)只需替换您得到的字符串中的逗号
- 在加载 VM 时设置语言环境
回答by crunchdog
Java has excellent support for formatting numbers in text in different locales with the NumberFormatclass:
Java 非常支持使用NumberFormat类在不同语言环境中格式化文本中的数字:
With current locale:
使用当前语言环境:
NumberFormat.getNumberInstance().format(5000000);
will get you (with swedish locale) the string: 5 000 000
将为您提供(使用瑞典语语言环境)字符串:5 000 000
...or with a specific locale (e.g. french, which also results in 5 000 000):
...或使用特定的语言环境(例如法语,也会导致5 000 000):
NumberFormat.getNumberInstance(Locale.FRANCE).format(5000000);
回答by Marco
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
我使用此方法将 double 格式化为具有固定语言环境的字符串,没有分组,并且具有最小和最大小数位数
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;
}
回答by jirkarrrr
This will remove all grouping (in your case commas).
这将删除所有分组(在您的情况下为逗号)。
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);
回答by Mohammad amarneh
NumberFormat format = NumberFormat.getInstance();
NumberFormat 格式 = NumberFormat.getInstance();
format.setGroupingUsed(false);
format.setGroupingUsed(false);