Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
148
rated 0 times [  153] [ 5]  / answers: 1 / hits: 7056  / 10 Years ago, wed, july 30, 2014, 12:00:00

I have



var x = 0100;

parseInt(x); // returns 100;


Is there a way I can retain the leading 0s and yet convert to number in Javascript


More From » javascript

 Answers
2

Unfortunately it's not possible but I have a solution that can help you, is to define a class from where you can get the value as integer and when you call the toString method it returns the value with leading zeros



function Num ()
{
this.value = arguments[0];
this.val = this.valueOf = function ()
{
return parseInt(this.value);
}
this.toString = function()
{
return this.value;
}
}


Now what you can do is to define a new number like this :



var x = new Num(00011);
x.val() // returns 11
x.toString() // returns 00011

[#43467] Wednesday, July 30, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;