简体   繁体   English

Windows Phone和仿真器之间的不一致

[英]Inconsistency between Windows Phone and Emulator

To tell shortly what I experienced; 简短地说出我的经历; I have a String in a floating number form to be converted into Double. 我有一个浮点数形式的字符串要转换为Double。 For Windows Phone 7 Device, it can only convert the numbers fractioned with "comma" (eg 46,211), For Emulator 8, it can only convert with "dot" (eg 46.211). 对于Windows Phone 7设备,它只能转换以逗号分隔的数字(例如46,211),对于仿真器8,它只能转换为“点”(例如46.211)。 If you do contrastly, one of them crashes. 如果您做相反的事情,其中​​之一会崩溃。

Why such a simple crappy thing occurs? 为什么会发生如此简单的cr脚事件? Anyone noticed this before? 有人注意到吗?

void getconditions(string evaulate)
{
    int i = 0;
    string[] evaluatearray = evaulate.Split(new Char[] { ':' });

    foreach (string s in evaluatearray)
    {
        conditions[i] = Convert.ToDouble(s);
        i++;
        if (i == conditions.Length) break;
    }
}

So in this code, s contains fractioned number strings, then I convert them into double as shown above. 因此,在此代码中,s包含分数数字字符串,然后如上所述将它们转换为double。 Thanks 谢谢

This is because you're trying to parse the number without specifying the culture. 这是因为您试图在不指定区域性的情况下解析数字。 In those cases, the default culture will be used. 在这些情况下,将使用默认区域性。 What happens is simply that the language of the emulator is different of the language of your phone. 发生的事情仅仅是模拟器的语言与电话的语言不同。

As a rule of thumb, you should never call a .Parse method without specifying the culture. 根据经验,在未指定区域性的情况下,切勿调用.Parse方法。 The same rule applies when converting a number or a date to string by using the .ToString method. 使用.ToString方法将数字或日期转换为字符串时,将应用相同的规则。

double number;

// Use the invariant culture when the culture is irrelevant
// (typically, when you are parsing numbers generated by an application of your own)
number = double.Parse("46.211", CultureInfo.InvariantCulture);

// Use a specific culture in all the other cases:
number = double.Parse("46,211", CultureInfo.GetCultureInfo("en-GB"));
number = double.Parse("46.211", CultureInfo.GetCultureInfo("en-US"));    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM