user613326
user613326

Reputation: 2180

can someone explain this rounding behaviour

I dont understand why it behaves like this

a = "1,00"
IFormatProvider numberlanguagestyle = CultureInfo.CreateSpecificCulture("en-US");
pricetopay = (int)decimal.Parse(a, numberlanguagestyle);
Console.writeline(pricetopay)

this outputs 100

while

a = "1"
IFormatProvider numberlanguagestyle = CultureInfo.CreateSpecificCulture("en-US");
pricetopay = (int)decimal.Parse(a, numberlanguagestyle);
Console.writeline(pricetopay)

this outputs 1

Now this works fine in my build of vs2010, but we have different programmers using different languages and we got into discussions about rounding errors and stuff like that While above code works correct in our app, i wondered why it behaves like that.

Upvotes: 0

Views: 70

Answers (3)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391724

That is because , is used as number group separator, not as decimal point. Typically you would use this to group thousands, like 1,000,000 for one million. In the en-US culture you need to use . as decimal point.

Upvotes: 4

D Stanley
D Stanley

Reputation: 152644

I may be misunderstanding your question (since I don't see what it has to do with rounding), but in the US the decimal separator is ".", not ",", so the parser is (correctly) interpreting 1,00 as 100.

Upvotes: 2

Jeffrey Sax
Jeffrey Sax

Reputation: 10323

In the en-US culture, the comma is a thousands-separator, so it is basically ignored. "1,00" is therefore interpeted as "100", which is, well, 100.

Upvotes: 3

Related Questions