C# 将文本框转换为带区域性信息的十进制
我面临一个问题,我的texbox值是小数。 在比利时,十进制字符是“,”。它在UI上工作得很好,但是一旦我将这些值传递给我的方法,它就不再读取数据了 我试着玩文化信息,但运气不好 在这里,它仍然使用逗号作为分隔符来提供正确的值。例如,这里仍然是“85,00”的正确值 但这里的价格PriceXVAT和priceIncVat现在是“8500” 我的理解是,当我使用CultureInfo.InvariantCulture时,字符串被转换为正确的区域性信息。但我认为我错了 因为您从未在代码中使用当前区域性或特定区域性(可以是C# 将文本框转换为带区域性信息的十进制,c#,decimal,cultureinfo,C#,Decimal,Cultureinfo,我面临一个问题,我的texbox值是小数。 在比利时,十进制字符是“,”。它在UI上工作得很好,但是一旦我将这些值传递给我的方法,它就不再读取数据了 我试着玩文化信息,但运气不好 在这里,它仍然使用逗号作为分隔符来提供正确的值。例如,这里仍然是“85,00”的正确值 但这里的价格PriceXVAT和priceIncVat现在是“8500” 我的理解是,当我使用CultureInfo.InvariantCulture时,字符串被转换为正确的区域性信息。但我认为我错了 因为您从未在代码中使用当前区域
fr be
或nl be
,因为您没有指定它)
由于InvariantCulture
使用,
作为分隔符,您的代码认为此分隔符是千位分隔符,而不是十进制分隔符
这就是为什么你的结果将是8500
而不是85,00
(我假设你的85000
是打字错误)
如果在代码中使用特定的文化,我认为应该可以
decimal.Parse("85,00", CultureInfo.GetCultureInfo("fr-BE"));
// 85,00
decimal.Parse("85,00", CultureInfo.GetCultureInfo("nl-BE"));
// 85,00
谢谢实际上,我不知道不变量文化使用“,”作为数字组分隔符。我在某个地方读过,它可以像这样使用,它可以从机器上获取信息。但表面上并非如此。那是坏消息。
public void addProduct(string productCode, string productName, string productDescription, decimal priceExVat, decimal priceIncVat, string serialNumber, bool isService)
{
tbl_products product = new tbl_products();
product.ProductCode = productCode;
product.ProductName = productName;
product.ProductDescription = productDescription;
product.ProductPriceExVat = priceExVat;
product.ProductPriceInclVat = priceIncVat;
product.ProductSerialNumber = serialNumber;
product.IsService = isService;
decimal.Parse("85,00", CultureInfo.GetCultureInfo("fr-BE"));
// 85,00
decimal.Parse("85,00", CultureInfo.GetCultureInfo("nl-BE"));
// 85,00