473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# Casting error

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.
Jul 21 '05 #1
5 1498
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" <an*******@discussions.microsoft.com> wrote in message
news:21**********************************@microsof t.com...
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.

Jul 21 '05 #2
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.
Jul 21 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
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.

Jul 21 '05 #4
Bryce <an*******@discussions.microsoft.com> wrote:
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 '.'.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
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" <an*******@discussions.microsoft.com> a écrit dans le message de
news:21**********************************@microsof t.com...
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.

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.