Convert String With Zeros To Decimal

I have a string like this: "000123".

I want to know how to convert this string to decimal but keep the leading zeros. I have used Convert.ToDecimal(), Decimal.TryParse & Decimal.Parse. But all of those methods keep removing the leading zeros. They give me an output: 123. I want the decimal returning 000123. Is that possible ?

Jon Skeet
people
quotationmark

No, it's not. System.Decimal maintains trailing zeroes (since .NET 1.1) but not leading zeroes. So this works:

decimal d1 = 1.00m;
Console.WriteLine(d1); // 1.00
decimal d2 = 1.000m;
Console.WriteLine(d2); // 1.000

... but your leading zeroes version won't.

If you're actually just trying to format with "at least 6 digits before the decimal point" though, that's easier:

string text = d.ToString("000000.#");

(That will lose information about the number of trailing zeroes, mind you - I'm not sure how to do both easily.)

people

See more on this question at Stackoverflow