区域设置独立的字符串翻番 [英] Locale independent String to double

查看:154
本文介绍了区域设置独立的字符串翻番的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有编辑文本框,用户输入的应用程序使用运行计算的十进制数。

In my app, I have edit text boxes where the user enters a decimal value that the app uses to run a calculation.

在其他区域设置一些用户输入数字时,如入值和不同的一个报告中出现的问题。我试图通过获得来自EDITTEXT值用这种方法来解决这个问题:

Some users in other locales are reporting problems when entering the numbers, like entering a value and a different one appearing. I have tried to fix this by getting the value from the editText with this method:

public double stringToDouble(String s){
        if (nf == null){
            nf = NumberFormat.getInstance(Locale.US);
        }
        try {
            return nf.parse(s).doubleValue();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0.0;
        }
    }

val = stringToDouble(et.getText().toString());

但显然仍是不工作的一些人。

But apparently that still isn't working for some people.

在斯洛文尼亚的一位用户报告说,它工作正常,如果

One user in Slovenia reported that it works fine if

      
  • 设置;语言和放大器;键盘:
  •   
  • 选择语言:设置为英语(斯洛文尼亚)
  •   
  • 触摸输入:只有设置为英语和任何语言(我的情况   斯洛文尼亚)
  •   
  • 双语prediction:OFF
  •   
  • 正文prediction:OFF
  •   
  • settings; Language & keyboard :
  • Select language : set to English(Slovenia)
  • Touch input: set only to English and whatever language(my case Slovenia)
  • Bilingual prediction: OFF
  • Text prediction: OFF

什么是去获取的双重价值的正确方法是什么? 谢谢

What is the correct way to go about fetching the double values? Thanks

推荐答案

从外观上来看,做正确的做法是:

From the looks of it, the correct way to do it is:

public double stringToDouble(String s){
        if (nf == null){
            nf = NumberFormat.getInstance(Locale.getDefault());
        }
        try {
            return nf.parse(s).doubleValue();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0.0;
        }
    }

到目前为止,这似乎是为我工作。

So far that seems to be working for me.

这篇关于区域设置独立的字符串翻番的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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