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
            if (fax3.Value == string.Empty)
                return 0;
            else return decimal.Parse(fax3.Value);


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

//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.
CamilliaAuthor Commented:
>>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?
CamilliaAuthor Commented:
and what if i dont change the decimal. at least not now. How can I fix this??
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

CamilliaAuthor Commented:
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!";
There is no "logical" difference between a phone number and a string, or a SSN or a zip code for that matter.

If you are not going to perform math on it then it should not be stored as a numeric type. I'm sure you could find decenting opinion but I believe I'm in the consensus here.

Anyway if you want leading zero's you haven't a lot of choice because when using a numeric type 0 is the same as 00000000000000000000. c# is not going to use 10 zero's to represent something that is only 1 zero. Simple logic. They didn't write any "helper" functions to parse it that way because logically it makes no sense to do so 0 is 0 when you're doing math.

<see snippet for optional approach>

private string FaxNum3
            if (fax3.Value == string.Empty)
                return 0;
            //zero is the string format placeholder for number of digits
            //its only a coincidence that you are asking about zeros as well
            //the number of zeros is the number of placeholders so
            // "000000000" would leave a longer result than "0000" etc
            else return fax3.Value.ToString("0000");


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day 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...
CamilliaAuthor Commented:
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....
CamilliaAuthor Commented:
This works String("0000");

Thanks, as always, for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.