2

I try to convert a number with leading zero. JavaScript interpret the number with leading zero as octal number. But I would like to convert the number to string as decimal number and preserve the leading zero. Any idea?

<!DOCTYPE html>
 <html>
 <body>

<p>Click the button to display the formatted number.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
    function myFunction() {
    var num = 015;

    var n = Number(num).toString();
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>
CK8
  • 347
  • 7
  • 22
  • 2
    a numeric literal with a leading zero and no digits greater than 7 will always be interpreted as an octal ... so don't do that with literals – Jaromanda X Dec 29 '16 at 02:26
  • Thanks, @Jaromanda. I have never known that. – vothaison Dec 29 '16 at 02:39
  • The num field is a input field, sometime there is leading zero in the field. – CK8 Dec 29 '16 at 02:40
  • no, in the code you posted, the num **var** is an octal literal. Perhaps you should've posted the actual code, and explained that "sometimes the input field has a leading zero" - because you'd want to parseInt(stringValue, 10) to ensure the number is considered to be decimal rather than octal – Jaromanda X Dec 29 '16 at 02:44
  • For padding a number with leading zeros, see [*Pad a number with leading zeros in JavaScript*](http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript). – RobG Dec 29 '16 at 02:44

2 Answers2

1

try below code

function pad(num, size) {
        var s = num + "";
        while (s.length < size) s = "0" + s;
        return s;
    }
    document.getElementById("demo").innerHTML = pad(15, 4);

output: 0015

0

The parseInt() and parseFloat() functions parse a string until they reach a character that isn't valid for the specified number format, then return the number parsed up to that point. However the "+" operator simply converts the string to NaN if there is an invalid character contained within it. Just try parsing the string "10.2abc" with each method by yourself in the console and you'll understand the differences better.

+"42"; // 42
+"010"; // 10
+"0x10"; // 16

a = "0042";
alert(typeof +a); // number
alert(+a); // 42

Yichong
  • 707
  • 4
  • 10
  • when I try parseInt(015,10).toString it return 13 – CK8 Dec 29 '16 at 03:11
  • Thanks, Sorry the issue was a is number with leading 0 for example a=042. and a is not string if I do – CK8 Dec 29 '16 at 03:21
  • var a=042; if I do alert(+a) it return 13 – CK8 Dec 29 '16 at 03:25
  • Sorry I meant var a=015 if I do alert(+a) it return 13. – CK8 Dec 29 '16 at 03:30
  • a number with a leading 0 will always be treated as an octal in JS, so you need to use a string instead, which will be converted to a decimal number if you put a "+" operator before it. – Yichong Dec 29 '16 at 03:48
  • Y.C. I I try var a=015 alert(String(+a)); it return NaN; – CK8 Dec 29 '16 at 03:55
  • Y.C. if I try var a=015; alert(String(+a)); // NaN – CK8 Dec 29 '16 at 03:59
  • @CK8 var a=015; alert(+a); // 13 var a="015"; alert(+a); // 15 – Yichong Dec 29 '16 at 04:04
  • But var a = 015 is given from other result, I can't change to var a="015". How to change from number to string with leading zero with octal to decimal conversion? – CK8 Dec 30 '16 at 03:00
  • Sorry I meant how to change a number to string with leading zero without octal to decimal conversion? – CK8 Dec 30 '16 at 03:02