双打,逗号和点 [英] Doubles, commas and dots

查看:177
本文介绍了双打,逗号和点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Android的Java程序,它从用户采取双重价值。如果我的计算机上运行的程序是可行的,因为我的电脑,EN_UK的语言环境的伟大。但是,当我在我的手机与FI_FI语言环境中运行它,它不会工作。我知道原因:在英国人们使用点作为小数点分隔符,但这里在芬兰小数点分隔符是逗号。

I'm making an Android Java program which is taking double values from the user. If I run the program on the computer it works great because of the locale of my computer, EN_UK. But when I run it on my mobile phone with FI_FI locale, it won't work. I know the reason: In UK people use dot as decimal seperator but here in Finland the decimal seperator is comma.

DecimanFormat df = new DecimalFormat("#.#");
Double returnValue = Double.valueOf(df.format(doublenumber));

在我使用的是逗号,它说 java.lang.NumberFormatException:无效的双:1234,5

When I'm using comma, it says java.lang.NumberFormatException: Invalid double: "1234,5".

我怎样才能使其与他们两个,逗号和工作点?

How can I make it work with them both, comma and dot?

推荐答案

使用的DecimalFormat的其他构造函数之一:

Use one of the other constructors of DecimalFormat:

new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US))

然后再尝试同时使用分离器对其进行解析。

And then try and parse it using both separators.

这篇关于双打,逗号和点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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