Java:浮点格式取决于语言环境 [英] Java: Float Formatting depends on Locale

查看:136
本文介绍了Java:浮点格式取决于语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我住在比利时。一般来说,在数学中,我们使用逗号来表示我们的小数:3,141592

这也是当我格式化浮点数时的结果。 b
$ b

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

因此,被a 就像这样: 3,141592 。所以总是当我需要一个点时,我不得不添加如下所示: String.format(%f,3.14).replace(',','。'); $ / $> b
$ b

谢谢




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

打印

  nl_BE 


解决方案 c> String.format(Locale.US,%f,floatValue)用于设置在格式化过程中使用的语言环境。


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

解决方案

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

这篇关于Java:浮点格式取决于语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆