Results 1 to 6 of 6

Thread: System.Decimal Bug or By Design, how to fix the design.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    127

    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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    127
    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    127
    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

  5. #5
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    here we use the comma instead of the point
    \m/\m/

  6. #6
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63
    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
  •  



Click Here to Expand Forum to Full Width