Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent automatic truncation of leading zeros in Excel cell

Tags:

If I paste 04562, it automatically truncates the leading zero and stores it as 4562.

I want it to to store it as 04562.

like image 783
Rajasekar Avatar asked Feb 14 '10 16:02

Rajasekar


2 Answers

If you are using MS Excel to edit the cells where you need to store the numbers with leading zero, just format and define the cell content as TEXT. If you are storing it programmatically, you can put the value in a quote '' for the same.

Update

I just noticed that if I go to "Format Cells" and select custom format and put, say 0000 to the format, any number that I put in the cell, it is is less than 4 digits, it still maintain the 4 digit length with the leading '0', like 0001, 0099, 0120, 9999

like image 126
Kangkan Avatar answered Oct 24 '22 19:10

Kangkan


While all of this is accurate workarounds for converting a number to text so that the leading zeros will be preserved, it is not an answer to the question.

The answer to your question is that if you want a number with leading zeros in Excel, you cannot have it - at least during the import.

The Microsoft developers, for whatever reason built Excel to read in 03 as a number and truncate the leading zero, resulting in 3. There is no way to prevent this.

It is unfortunate because all workarounds assume you know what columns have values that might have leading zeros so that you can take steps * after the fact * to try and fix your data.

By the way, Google Sheets does the same thing.

So the short answer is that there is no spreadsheet application (at least that I am aware of) where you can read in a csv file that might have oh, say 00453 as a number and have it imported as 00453 and not 453.

like image 20
Randy Volters Avatar answered Oct 24 '22 18:10

Randy Volters