Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

decimal.parse changes 4 digit zeros

I have a field that in the database it's decimal. I have 4 digit of 0000.

When I do decimal.parse, it turns the 4 digit zero into one Zero.

How can I fix this? FaxNum3 turns into one digit zero when I pass it 4 digit zero


if (string.Concat(FaxNum1, FaxNum2, FaxNum3).Length != 10)
                lFaxComplete.Text = "Incomplete fax number!";

--------------------
private decimal FaxNum3
    {
        get
        {
            if (fax3.Value == string.Empty)
                return 0;
            else return decimal.Parse(fax3.Value);
        }

    }

Open in new window

Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

0000 == 0

Decimal doesn't have a parse or format option that will do what you want because mathematically that just doesn't have any context or make sense.

Of course you could use string formatting to make it show 4 zeros but that isn't what you're asking:

EX.
//will produce 0005
// 0 will produce 0000
decimal d = 5;
string test = d.ToString("0000");


The correct answer may not be what you want but still.

Storing a fax number as a decimal in your database is a mistake. It may use numbers but it is not a stragegic numeric type (you aren't going to perform math on it).

It is a string and should be stored as such (varchar or nvarchar) in database. Then your c# property can be the right type and you can format it any way you want.
Avatar of Camillia

ASKER

>>Of course you could use string formatting to make it show 4 zeros but that isn't what you're asking:

Yes, that's what I want. I want to make sure 10 digits are entered, and if there are 4 zeros, then i want to make sure the length is 4. How do I do this? with this: string test = d.ToString("0000");


>>striIt is a string and should be stored as such (varchar or nvarchar) in database.
a number , phone number, should be stored as varchar?
and what if i dont change the decimal. at least not now. How can I fix this??
I used the actual field value instead of calling the property and it worked

 if (string.Concat(fax1.Value, fax2.Value, fax3.Value).Length != 10)
                lFaxComplete.Text = "Incomplete fax number!";
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I used the actual field value instead of calling the property and it worked<<

And that's fine. I thought the issue was bigger than getting a piece of validation to work.

Now what are you to if someone puts in a phone number like 0015 when the db changes that to 15? You are going to need to use .ToString("0000") to fix it.

As long as you're happy...
You're right, if i enter 0015, DB changes it to 15.

You know what, my field is not decimal. My phone fields are money. I actually read that on a site to make phone numbers money. Should I change it to varchar?
Long discussions can be had about how to store phone numbers in a database.

Most will say use up to varchar(22) which is the maximum size needed for an international phone number. Pick your maximum size based on what your user base will require and use that is a very popular opinion.

Normalize (remove any formatting) and store it. When you store it this way all your data is the same and you can easily use all sorts of string formatting to display your output. It makes things flexible and consistent.

There are a few arguments that say use a number because it uses slightly less space. The argument here is not strong and I prefer the answer of varchar. Not to mention using a number requires casting and manipulation (as you've already seen) that is a pain and all because what you're really doing with the value is not math.

For what its worth....
This works String("0000");

Thanks, as always, for your help.