字符串 "1.23E-2" 需轉為 0.0123 (Decimal)。
private decimal ParseScientific(string s)
{
if (s.Contains("E") || s.Contains("e"))
{
// NumberStyles.Float 支持指數表示法
return decimal.Parse(s, System.Globalization.NumberStyles.Float);
}
return decimal.Parse(s);
}
var tests = new[] { "1.23E-2", "5E2", "1.5e3", "123.45" };
foreach (var t in tests)
Console.WriteLine($"{t} -> {ParseScientific(t)}");
// 1.23E-2 -> 0.0123
// 5E2 -> 500
// 1.5e3 -> 1500
// 123.45 -> 123.45
NumberStyles.Float 包含 AllowExponent 標誌,自動解析科學計數法。decimal 保證財務精度,避免 double 浮點誤差。NumberStyles.AllowThousands。