Decimal d1 = Decimal.Parse("0.1"); // = 1?!?

  • Thread starter Thread starter leej
  • Start date Start date
L

leej

Im hoping someone can help me to work out where/what Im missing here.
Ive a line in a moderately sized program

decimal d1 = Decimal.Parse("0.1");

When I check the result in Visual Studio or perform any math using d1,
the value of d1 is 1.

Thinking I was missing something obvious (it is late here), I changed
d1 the line to

Decimal decVariable = Decimal.Parse("0.8");

And the result was 8.

Things became even more bizarre when I changed the Decimal to a Double
with the same result and then wrote a seperate little test app and got
the results I expected.

Is there anything obvious I am missing here? Its been a while since I
worked with c#/.net (this is VS 2005/.Net 2.0) and am sure its
something Im doing wrong...

Tia
 
10 minutes away from the PC and I worked it out - the app is localised
and Id switched to a German locale!

That said - now Im wondering how to deal with settings like this when
they are user configurable via app.config.........
 
You should use the overload that lets you specify IFormatProvider used for
parsing the strings

decimal d = decimal.Parse("2.3",
System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
 
Im hoping someone can help me to work out where/what Im missing here.
Ive a line in a moderately sized program

decimal d1 = Decimal.Parse("0.1");

When I check the result in Visual Studio or perform any math using d1,
the value of d1 is 1.

Thinking I was missing something obvious (it is late here), I changed
d1 the line to

Decimal decVariable = Decimal.Parse("0.8");

And the result was 8.

Things became even more bizarre when I changed the Decimal to a Double
with the same result and then wrote a seperate little test app and got
the results I expected.

Is there anything obvious I am missing here? Its been a while since I
worked with c#/.net (this is VS 2005/.Net 2.0) and am sure its
something Im doing wrong...


Hi Tia,

I'm using VS2005 too. I copy and pasted your code and it is working fine
for me. Did you try copying it out of your current project and pasting it
into a new project to see the results. Maybe try debugging by commenting
out all the algorithm except for that line, run the code to validate
results, and continue uncommenting and running again until you find where
the problem is.

Joe
 
10 minutes away from the PC and I worked it out - the app is localised
and Id switched to a German locale!

That said - now Im wondering how to deal with settings like this when
they are user configurable via app.config.........

Makes perfect sense to me now. Please ignore my previous post. Lebesgue
got it right.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top