double.Parse显然不适用于某些国家/地区 [英] double.Parse doesn't work with some country apparently
问题描述
基本上,我有一个非常简单的计算:
Basically, I have this really simple calculation:
int bTaxPrice = int.Parse(prices[wep]);
double t = double.Parse("1." + taxes);
double price = Math.Round(t * bTaxPrice);
我将为您提供一个示例,说明计算的工作方式,先说t=1.1
和bTaxPrice=1279
,然后说1.1*1279 = 1406.9
,但是由于四舍五入的结果,价格等于1407.
I'll give you an example for how the calculation should work, lets say t=1.1
and bTaxPrice=1279
, then 1.1*1279 = 1406.9
, but since I'm rounding the result the price equals 1407.
对于使用我的C#Winforms程序的另一个国家(丹麦)的某些用户,遇到了一个问题,该问题导致数字不能四舍五入,而是将小数点后的最后两位数字相加.
For some users from another country (Denmark) that are using my C# Winforms program are experiencing a problem that causes the number not to round, but to add the last 2 digits after the decimal point.
我们上述计算的结果是1407,对于他们来说是140690.
Our result for the calculation above was 1407, for them it's 140690.
我在互联网上读到了它,有些国家的小数点有些特殊.
I read about it in the internet, and some countries have something special with there decimal point.
什么是解决此类问题的好方法?
What is a good fix for this kind of issue?
推荐答案
实际上,许多国家/地区使用逗号作为小数点分隔符.
如果要使用点作为分隔符,请使用不变的CultureInfo
:
Actually, many countries use a comma for their decimal separator.
If you want to use the dot as a separator, use the invariant CultureInfo
:
double.Parse("1." + taxes, CultureInfo.InvariantCulture);
这篇关于double.Parse显然不适用于某些国家/地区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!