0

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 ?

Conor Gallagher
  • 1,500
  • 2
  • 19
  • 42
  • 3
    No, it's not. A number doesn't have any formatting information by itself. Why do you need to do this? You can always format it to show in a specific way. – Sami Kuhmonen Mar 03 '17 at 07:24
  • of course because decimal numbers do always convert it as to absolute decimal type – Shift 'n Tab Mar 03 '17 at 07:24
  • Possible duplicate with http://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros – rmjoia Mar 03 '17 at 07:25
  • Why would you keep the leading zero? For futur displaying? – dbraillon Mar 03 '17 at 07:25
  • 1
    `...000123 == 00123 == 0123 == 123` if you want to represent `123` as `"000123"` use formatting or `PadLeft` – Dmitry Bychenko Mar 03 '17 at 07:26
  • if you want to display a zerofill format you can always do the object method `.ToString("000000")` – Shift 'n Tab Mar 03 '17 at 07:27
  • 1
    As an aside, are you sure this should be decimal rather than int? – Jon Skeet Mar 03 '17 at 07:28
  • Possible duplicate of [C# convert int to string with padding zeros?](http://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros) – MakePeaceGreatAgain Mar 03 '17 at 07:31
  • @SamiKuhmonen: Hi, i m in the middle of creating console app for dynamics crm. The main purpose of console application is to copy records from crm online and paste records to crm 365 on -premise. Because some of field in both crm are not same, i get a problem when i need to paste a string data type into decimal data type. – user3284126 Mar 03 '17 at 07:33
  • Then you'll just convert it to the proper type and if you need leading zeroes you modify the presentation format for it. – Sami Kuhmonen Mar 03 '17 at 07:34
  • @dbraillon: Based on my comment above, The records is sensitive, so i need to keep all value without any changes – user3284126 Mar 03 '17 at 07:34
  • 2
    You´re mixing **data** and **representation**. Whilst the former is mostly sensible the latter is free to your needs without changing your data. So you can just print your data in any format you like. – MakePeaceGreatAgain Mar 03 '17 at 07:37
  • Thanks for you response all, by knowing that its not possible to convert my string format to decimal. I think i need to talk to crm admin to change the field type to string. – user3284126 Mar 03 '17 at 07:43
  • Or change your use of the data later. We don't have much context about what you're using this information for. Will you ever have both "0123" and "000123"? Do you actually need to distinguish between them? Is this *really* just an integer you're trying to track? – Jon Skeet Mar 03 '17 at 07:58
  • 2
    Basically if you don´t need/can´t calculate with the numbers and they are more *identifiers* than actual *numbers* you should consider to store them as strings into your database. An example for this might be zip-codes or even phone-numbers where you often have leading and trailing zeros which belong to the identifier. However you´ll never **calculate** with a phone-number, will you? – MakePeaceGreatAgain Mar 03 '17 at 08:02
  • The statement "I want the decimal returning 000123" is unobtainable as in .Net the decimal does not maintain these leading zeros. What you have is a string representation of the decimal. As others have mentioned, what are you doing with the data? If you are using it for calculations while maintaining the format, then we could go into many different different ways of skinning that cat so to speak. Right up to defining your own "MySpecialDecimal" class that holds a decimal version of the string in the background and is operator overloaded within an inch of its life to allow calculations. – Conor Gallagher Mar 08 '17 at 23:18

2 Answers2

2

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

So you need to store 000123 in a decimal variable, First of all it is not possible since 000123 is not a Real Number. we can store only Real numbers within the range from -79,228,162,514,264,337,593,543,950,335 to +79,228,162,514,264,337,593,543,950,335 in a decimal variable. No worries you can achieve the target, decimal.Parse() to get the value(123) from the input(as you already did) and process the calculations with that value. and use .ToString("000000") whenever you wanted to display it as 000123

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • What do you mean by "000123 is not a Real Number"? I.m.o it is a perfectly valid number, only its leading zeroes are not significant. When the input string is guaranteed to contain a numeric value, trying to preserve the leading zeroes is simply pointless. – Henk van Boeijen Mar 03 '17 at 17:42
  • I would imagine folk in the maths section might argue otherwise (e.g. http://math.stackexchange.com/questions/1473877/confusion-about-natural-numbers-and-leading-zeros). I would also agree and say 000123 is not a legal numerical representation of a natural or real number. It's a perfectly valid string representation though. – Conor Gallagher Mar 08 '17 at 23:13