Pat*_*son 0 .net c# xamarin xamarin.forms
我做了一个简单的应用程序,将一定数量的比特币转换为货币.
该应用程序在我的Android模拟器(Nexus 5x)上工作正常.我的问题是该应用程序抛出:
System.FormatException:输入字符串的格式不正确
当我在我的Android设备上测试它.我已经对我的模拟器和设备进行了调试,并且在参数方面没有差异.
有人有任何想法为什么会发生此异常?
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;
}
Run Code Online (Sandbox Code Playgroud)
那个逗号分隔符是你的问题.您需要更改解析方法以检测当前文化并使用它进行解析IFormatProvider
使用以下的重载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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
445 次 |
最近记录: |