-2

I am working with parseInt() function in JavaScript and need to apply logic as if given number is less than 10 then add 0 before number.

So, if given number is 9 then print it as 09. What i apply is:

if (no < 10) {
    no = "0" + no;
}

and the apply parseInt() method on it but every time leading zero is flash-out.

Kiran
  • 469
  • 1
  • 7
  • 17
  • If it's already a number why do you need to involve `parseInt()` at all? – Pointy Jun 22 '15 at 11:26
  • 1
    thats what parse int will do. why do you need pars int **after** you have prefixed the value? – atmd Jun 22 '15 at 11:26
  • no is converted to string, when assigning string. – Nina Scholz Jun 22 '15 at 11:26
  • Maybe this [one](http://stackoverflow.com/questions/2686855/is-there-a-javascript-function-that-can-pad-a-string-to-get-to-a-determined-leng) helps :) – Dimi Takis Jun 22 '15 at 11:26
  • You must do the reverse, parseInt() and then do the padding –  Jun 22 '15 at 11:29
  • parseInt() was there when i got code to apply, So i cannot remove it, so need to add logic or patch to figure out this. – Kiran Jun 22 '15 at 11:30
  • @Kiran: if you want to **redefine** parseInt() -- as your last comment suggests -- please edit your question accordingly (even if it sounds like a rather bad idea). – Arnauld Jun 22 '15 at 11:38

3 Answers3

2

In javascript, an integer (Number) cannot have a leading zero. If you want a leading zero, you should present it as a string.

parseInt('01', 10); // 1
parseFloat('01'); // 1
parseInt(01, 10); // 1

A useful function for adding padding to a number converted to a string. Feel free to put this in your own utils or other helper-toolbelt. Happy padding!

 /**
 * Add padding (leading zero's) to integer, based on minimum length
 * @param {Number} integer 
 * @param {Number} minimal length of returned string
 * @return {String} padded string
 */
function addPadding(integer, length){
  var integerString = integer + '';
    while (integerString.length < length) {
      integerString = '0' + integerString;
    }
  return integerString;
}

// Output examples
addPadding(15, 3);  // 015
addPadding(4, 2);   // 04
addPadding(123, 2); // 123
addPadding(123, 5); // 00123
Justus Romijn
  • 15,699
  • 5
  • 51
  • 63
1

You'll have to convert it to a string since numbers don't make sense with leading zeros.

So you must print the number as:

if (no < 10) {
    console.log("0" + no);
}

If no = 8, then result is "08"

Nikhil Batra
  • 3,118
  • 14
  • 19
0

First, always use a base of 10 with parseInt().

Second, use console.log() to see what you get:

no = parseInt(user_input, 10); // base 10
console.log(typeof no);
if (no < 10) {
    no = "0" + no;
}
console.log(typeof no);

This should give you in the console output:

number
string

A leading 0 in the input may get in the way as it is an octal representation and 8 or 9 are not a valid numerals. The output cannot have a leading 0 as a number and as such, must be kept as a string.

The last point is not a problem if you correctly separate the actual number from the numeral in your code:

no_to_compute = 8;         // is a number type
no_to_display = "0" + no;  // ia a string type:
                           // representation of the number (numeral)
pid
  • 11,472
  • 6
  • 34
  • 63