C# Casting error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

First off, I hope I am posting this in the correct place

Convert.ToDecimal() is throwing a FormatException, as is Decimal.Parse() if I send in the string "000063.27" or "+000063.27". I tried removing the zeroes (via substring) and this doesn't work either

Any ideas

Thanks in advance.
 
Are you sure? It works here just fine:

string s = "+000045.34";
System.Decimal d = Convert.ToDecimal(s);

Maybe you're casting the result to something else and that's what's causing
the error?


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Bryce said:
First off, I hope I am posting this in the correct place.

Convert.ToDecimal() is throwing a FormatException, as is Decimal.Parse()
if I send in the string "000063.27" or "+000063.27". I tried removing the
zeroes (via substring) and this doesn't work either.
 
yah, it does work like that. I'm sorry, I meant to say I have a string variable that is the decimal. I've tried substringing the string so only the two numbers in front of the decimal ("63") and I still get the same error. The string displays fine in a text box, but I need to get the numeric equivelant. Is it somehow corrupt or something?

Thanks.
 
I don't understand. So only the decimal *what*?

Do you mean you want to get rid of the mantissa or something like that?

Some sample code that illustrates what you're trying to do would be helpful.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Bryce said:
yah, it does work like that. I'm sorry, I meant to say I have a string
variable that is the decimal. I've tried substringing the string so only the
two numbers in front of the decimal ("63") and I still get the same error.
The string displays fine in a text box, but I need to get the numeric
equivelant. Is it somehow corrupt or something??
 
Bryce said:
First off, I hope I am posting this in the correct place.

Convert.ToDecimal() is throwing a FormatException, as is
Decimal.Parse() if I send in the string "000063.27" or "+000063.27".
I tried removing the zeroes (via substring) and this doesn't work
either.

What culture are you in? If you don't specify Culture.Invariant as the
culture to use, it'll use the current culture which may have ',' as the
decimal separator rather than '.'.
 
What's the culture on the current thread (Culture.CurrentCulture)?

Decimal.Parse is culture dependent. So, for example, if your machine is
configured with a French or German culture, Decimal.Parse will require a
comma as decimal separator.

You can use Decimal.Parse(str, NumberFormatInfo.InvariantInfo) to get
culture independent parsing.

BTW, this a parsing problem, not a casting error.

Bruno.

Bryce said:
First off, I hope I am posting this in the correct place.

Convert.ToDecimal() is throwing a FormatException, as is Decimal.Parse()
if I send in the string "000063.27" or "+000063.27". I tried removing the
zeroes (via substring) and this doesn't work either.
 
Back
Top