双打,逗号和点双打、逗号

2023-09-05 11:28:54 作者:守护

我在做一个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.