Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double.Parse doesn't work with some country apparently

Tags:

c#

math

rounding

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);

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.

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.

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?

like image 836
zarko Avatar asked Sep 14 '25 07:09

zarko


2 Answers

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);

like image 174
Martin Walter Avatar answered Sep 17 '25 02:09

Martin Walter


Some countries use a comma as decimal separator. You could fix this by supplying CultureInfo.InvariantCulture to the double.Parse method.

like image 42
C.Evenhuis Avatar answered Sep 17 '25 00:09

C.Evenhuis