|
-
Oct 10th, 2003, 07:59 AM
#1
Thread Starter
Lively Member
System.Decimal Bug or By Design, how to fix the design.
I have a problem that reared its ugly head in a bad way I am looking for the proper way to solve this. I personally think this is a bug. I had an application that kept crashing on someone, it was throwing the error from a SQL statement on insert saying I had more values than parameters, I figured ok I messed up on and if statement somewhere when doing some form validation. The Code was not that complexjust a simple form input of 10 various items. Well 2 of the items are decimal or will be converted to decimal. Well I quickly found out Decimal is not a decimal of even the definition of a decimal. The problem was the person doing the data entry was putting a comma in for every thousand, Ok great but conversion to decimal you would think would catch that, a conversion to integer will catch that so why can you use comma's in decimal and not integers and so on.
Sample Code and results so you can see what I mean.
Code:
try
{
decimal decim = Convert.ToDecimal(InputText.Text);
ResultText.Text = "That is a decimal " + decim.ToString();
}
catch
{
ResultText.Text = "That failed. It is not a decimal";
}
Sample Data Passes as a decimal or Fails
1 Pass
1.0 Pass
1,0 Pass * <That should not pass as a decimal won't pass as Int32
1,345678,66 Pass * <That should not pass as a decimal won't pass as Int32
1.456,456456 Failed *Ok it should fail
Ok so now how to fix this, the only thing that comes to mind is
decimal X = Convert.ToDecimal(Text1.Text);
X = Convert.ToDecimal(X.ToString().Replace(",",""));
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
-
Oct 10th, 2003, 08:45 AM
#2
Thread Starter
Lively Member
Well I am guessing it is by design but what a stupid design, and inconsistant, those number will not pass integer checks but they will pass Decimal and Double and probably a few others in there. like a 1,1 will not work as an integer but will work as a decimal or a double. I wish that was dumented somewhere.
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
-
Oct 10th, 2003, 10:11 AM
#3
Originally posted by BOUND4DOOM
Well I am guessing it is by design but what a stupid design, and inconsistant, those number will not pass integer checks but they will pass Decimal and Double and probably a few others in there. like a 1,1 will not work as an integer but will work as a decimal or a double. I wish that was dumented somewhere.
This is going to sound stupid, but it makes sense.... in some countries, the comma is used to denote a decimal point.....shrug... I don't know where exactly, but I do know I've seen that before. That's why 1,1 will pass a decimal test, and not an integer test.... like I said, this is going to sound stupid.
IMHPO: Do the validation as the user enteres the number, if they puch in a comma, toss it out. Don't do it later, do it as close to the user as possible. Then, by the time you get to the point where you are ready to store the value, it's already in the correct format.
TG
-
Oct 10th, 2003, 10:19 AM
#4
Thread Starter
Lively Member
Yeah tech you are correct, it just suprised me, and I believe it is the dutch areas that do that. Which is why I tested the 1.456,456456 because in the countries that do use a decimal point as a thousand separator they use the comma the way we use a decimal point.
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
-
Oct 13th, 2003, 12:48 PM
#5
yay gay
here we use the comma instead of the point
\m/  \m/
-
Oct 14th, 2003, 06:36 AM
#6
Member
You can also do:
decimal decim = decimal.Parse(InputText.Text);
this method can be overloaded to accept culture specific formatting, i.e. commas.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|