Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string to int without losing the zero in the beginning

I tried int.parse, and convert class to convert a string to int.

While I'm converting. I'm losing the 0 in the beginning which i don't want.

Ex : 09999 becomes 9999 - I don't want this.

I want to keep it as it is.

How can i do that?

like image 648
DarthVader Avatar asked Jul 07 '11 18:07

DarthVader


People also ask

Does int remove leading zeros?

The int() method to remove leading zeros in Python While converting, it will automatically remove leading zeros in the string. Note that the string should only contain numbers and no letters, alphabets, or other symbols.

Does parseInt remove leading zeros?

The parseInt function parses a string argument and returns a number with the leading zeros removed.

How do you keep leading zeros in Java?

You just need to add "%03d" to add 3 leading zeros in an Integer. Formatting instruction to String starts with "%" and 0 is the character which is used in padding. By default left padding is used, 3 is the size and d is used to print integers.


2 Answers

myNumber.ToString("D5");

//D represents 'Decimal', and 5 is the specified amount of digits you want the number to be always. This will pad your value with zeroes until it reaches 5 digits.

like image 136
Nme Avatar answered Oct 21 '22 09:10

Nme


You cannot. An int is meant to represent a mathematical integer. The numbers 09999 and 9999 are exactly the same number, mathematically.

Perhaps there is a different problem here. Why do you need to do this? Maybe there's a better way to do what you want to do.

like image 33
NickAldwin Avatar answered Oct 21 '22 11:10

NickAldwin