Java:浮点格式取决于区域设置

发布于 2024-10-10 01:10:51 字数 501 浏览 0 评论 0原文

我住在比利时。一般来说,在数学中,我们用逗号来写小数,如下所示:3,141592
这也是我格式化浮动时的结果。

System.out.println(String.format("%f", 3.141592));

因此,., 替换,如下所示:3,141592。因此,当我需要一个点时,我总是必须添加如下内容: String.format("%f", 3.14).replace(',','.');

所以,问题是:有没有一种方法可以更改区域设置,使 Java 中的每个格式化程序都使用点而不是逗号?

感谢


System.out.println(Locale.getDefault());

印刷品

nl_BE

I live in Belgium. And generally, in mathematics, we write our decimals with a comma like this: 3,141592
And that is also the result when I format the float.

System.out.println(String.format("%f", 3.141592));

So, the . is replaced by a , like so: 3,141592. So always when I need a point instead I have to add something like this: String.format("%f", 3.14).replace(',','.');

So, the question is: is there a way to change the Locale which makes every formatter in Java use a point, instead of comma?

Thanks


System.out.println(Locale.getDefault());

prints

nl_BE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶色山野 2024-10-17 01:10:51

尝试使用 String.format(Locale.US, "%f", floatValue) 来设置格式化期间使用的区域设置。

Try using String.format(Locale.US, "%f", floatValue) for just setting locale used during formatting.

远昼 2024-10-17 01:10:51

一个简单的解决方案是将系统区域设置设置为美国或英国,但会广泛影响整个区域设置。例子。

Locale.setDefault(Locale.US);

既然您已经更改了问题,那么您只需使用打印方法指定本地即可。

System.out.println(String.format(Locale.US, "%f", 3.141592));

A simple solution, but would be wide reaching across the entire Locale, would be to set the system Locale to US or UK. Example.

Locale.setDefault(Locale.US);

Since you have changed the question, then you simply specify the local with your print method.

System.out.println(String.format(Locale.US, "%f", 3.141592));
狼亦尘 2024-10-17 01:10:51

看看可能有用
http://download.oracle.com/javase/6 /docs/api/java/text/DecimalFormat.html
其中包含许多选项,可在必要时使用 Locale 精确输出字符串

DecimalFormat 是一个具体的子类
格式化十进制的 NumberFormat
数字。它有多种功能
旨在使解析成为可能
并在任何区域设置中格式化数字,
包括对西方、阿拉伯语的支持,
和印度数字。它还支持
不同种类的数字,包括
整数 (123)、定点数
(123.4),科学记数法(1.23E4),
百分比 (12%) 和货币
金额(123 美元)。这些都可以
本地化。

通过定制例程路由输入和输出通常很有用。这是 JAMA 库 (http://math.nist.gov/javanumerics/jama/) 中的一个,

   public void print (PrintWriter output, int w, int d) {
      DecimalFormat format = new DecimalFormat();
      format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
      format.setMinimumIntegerDigits(1);
      format.setMaximumFractionDigits(d);
      format.setMinimumFractionDigits(d);
      format.setGroupingUsed(false);
      print(output,format,w+2);
   }

通过使用它,您可以确保将来的问题和增强功能可能得到解决

It may be useful to look at
http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
which contains a number of options for precise output of strings using Locale where necessary

DecimalFormat is a concrete subclass
of NumberFormat that formats decimal
numbers. It has a variety of features
designed to make it possible to parse
and format numbers in any locale,
including support for Western, Arabic,
and Indic digits. It also supports
different kinds of numbers, including
integers (123), fixed-point numbers
(123.4), scientific notation (1.23E4),
percentages (12%), and currency
amounts ($123). All of these can be
localized.

It is often useful to route input and output through bespoke routines. Here is one from the JAMA library (http://math.nist.gov/javanumerics/jama/)

   public void print (PrintWriter output, int w, int d) {
      DecimalFormat format = new DecimalFormat();
      format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
      format.setMinimumIntegerDigits(1);
      format.setMaximumFractionDigits(d);
      format.setMinimumFractionDigits(d);
      format.setGroupingUsed(false);
      print(output,format,w+2);
   }

By using this you can be sure that future problems and enhancements are likely to be addressed

爱冒险 2024-10-17 01:10:51

对于格式化数字,您确实不应该依赖 toString()。使用 String.format()NumberFormat 中的工厂方法或 DecimalFormat,它们允许您控制数字的格式化方式(前两个通过选择区域设置)而不依赖于某些全局设置。

For formatting numbers, you really should not rely on toString(). Use String.format(), the factory methods in NumberFormat, or DecimalFormat, which allow you to control how numbers are formatted (the first two by choosing a Locale) without relying on some global setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文