我有一个来自excel文件的数据表,但似乎不可能用点(而不是逗号)将文本字段转换为十进制。
我尝试使用文化,但结果是逗号或没有标点符号
例如“ 4,65”变为“ 465”或“ 4,65”,而不是“ 4.65”
strange认为这是工作代码
var price = decimal.Parse("4,65", new NumberFormatInfo() { NumberDecimalSeparator = "," });
price = 4.65
但这不起作用
var price = decimal.Parse(excelRow[0][7].ToString(), new NumberFormatInfo() { NumberDecimalSeparator = "," });
price = "4,65";
这是其他审判
decimal price = Convert.ToDecimal(excelRow[0][7].ToString());
var price =decimal.Parse(excelRow[0][7].ToString().Replace(',', '.'), CultureInfo.InvariantCulture);
ar price =decimal.Parse(excelRow[0][7].ToString(), new CultureInfo("en-US"));
decimal price =(decimal)excelRow[0][7];
string price = String.Format("{0:0.##}", (decimal) excelRow[0][7]);
string sPrice = excelRow[0][7].ToString();
decimal price = decimal.Parse(sPrice, new NumberFormatInfo() { NumberDecimalSeparator = "," });
答案 0 :(得分:0)
您可以尝试
using System;
using System.Globalization;
class Test
{
static void Main()
{
decimal d = decimal.Parse("1200.00", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
}
}