[英]Xamarin app works on emulator but not on device
I´ve done a simple app which convert an amount of Bitcoins to a currency. 我已经完成了一个简单的应用程序,可以将一定数量的比特币转换为货币。
The app works fine on my android emulator(Nexus 5x). 该应用程序可以在我的Android模拟器(Nexus 5x)上正常运行。 My problem is that the app throws:
我的问题是该应用抛出:
System.FormatException: Input string was not in a correct format
System.FormatException:输入字符串的格式不正确
When I test it on my android device. 当我在Android设备上对其进行测试时。 I´ve debuged both my emulator and device and it´s no diffrence in the parameters.
我已经调试了我的模拟器和设备,并且参数没有差异。
Do someone have any thought why this exception occurs? 有人想到为什么会发生此异常吗?
public static async Task<decimal> ConvertCurrency(string curr, decimal amount)
{
var currency = await GetCurrency(curr);
var stringRate = currency.Rate;
var rate = decimal.Parse(stringRate);//this is where the it crash
var result = rate * amount;
return result;
}
That comma delimiter is your problem. 该逗号分隔符是您的问题。 You would need to change your parse Method to detect the Current culture and parse using that
IFormatProvider
您将需要更改解析方法以检测当前区域性并使用该
IFormatProvider
解析
Use the overload(s) of decimal.Parse
: 使用
decimal.Parse
:
string stringRate = "118,130.4542";
decimal d = decimal.Parse(stringRate,
NumberStyles.Currency,
new CultureInfo("en-Us").NumberFormat);
//Output: 118130.4542M
d = decimal.Parse(stringRate, new CultureInfo("en-Us").NumberFormat);
//Output: 118130.4542M
Most likely that happens because of the language settings on your phone. 这很可能是由于手机上的语言设置引起的。 If you try to parse a string from the web using default decimal.Pars function, it would try to identify the separator based on your language settings (or local, it depends).
如果尝试使用默认的decimal.Pars函数从Web解析字符串,它将尝试根据您的语言设置(或本地,取决于它)来识别分隔符。 That mean that if a phone use a "," as a decimal separator, it would try to look for it into input string even if the string come in a format 12.32.
这意味着,如果电话使用“,”作为小数点分隔符,即使该字符串的格式为12.32,它也会尝试将其查找到输入字符串中。 To fix that, pass a culture or implicit separator into parser.
要解决此问题,请将区域性或隐式分隔符传递到解析器中。
decimal.TryParse(EnglishDecimal, out b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.