-3

I have this simple function below. I'm passing a number with a leading zero as an argument and the leading zero is getting stripped out of the number. How can I prevent that from occurring without passing in the number as a string? Is that possible?

function someFunction(number){
      console.log(number); // 25468 <--- leading zero removed
    }

    // pass in a number with leading zeros
    someFunction(025468);
shmuli
  • 5,086
  • 4
  • 32
  • 64
  • 8
    You will have to sent it as a string `someFunction('025468');` then when needed parse it to a number for computation – Arun P Johny Apr 28 '15 at 05:45
  • @ArunPJohny I know. I asked in my question, "without passing in the number as a string". – shmuli Apr 28 '15 at 05:46
  • 4
    Why does it matter? The number has the same value with or without the leading zero. If you need to format it for display, well, format it when you display it – Phil Apr 28 '15 at 05:46
  • 2
    This is almost certainly an XY-problem. What do you want to achieve? – Amadan Apr 28 '15 at 05:47
  • @Amadan I said what I want to achieve. Exactly that. – shmuli Apr 28 '15 at 05:48
  • @Phil: It doesn't matter in this instance; but `02546` is definitely not the same as `2546`... – Amadan Apr 28 '15 at 05:48
  • @Amadan I assumed OP would have mentioned if the number was octal. Also, it wouldn't have an `8` in it ;) – Phil Apr 28 '15 at 05:49
  • 3
    It is a silly thing to be a goal in and of itself; you are almost certainly trying to do something else where you think this would help. But the literal thing you are asking is impossible, so you need to get your ultimate goal another way. – Amadan Apr 28 '15 at 05:49
  • 2
    http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Phil Apr 28 '15 at 05:54
  • 1
    As soon as JS read and interpret `025468` it looses leading zeroes and there is no way to get them back. – Alexey Ten Apr 28 '15 at 06:07
  • @shmuli again, **why** does it matter? *"It matters"* is **not** a sufficient answer to that question – Phil Apr 28 '15 at 06:17

2 Answers2

2

How can I prevent that from occurring without passing in the number as a string? Is that possible?

No. Leading zeros are of no significance in the number system / representation JS uses and thus they are not stored in the internal representation of a number.

(or, if you like that better, a number has infinite leading zeros)

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Actually they are significant. `012 != 12`. In strict mode it will throw an error. – Alexey Ten Apr 28 '15 at 06:01
  • 1
    I assume you are talking about number *literals*, and yes they are important there. I'm talking about number *values* though. – Felix Kling Apr 28 '15 at 06:02
  • 1
    Yep. But OP wants to pass number literals. Anyway, he wants something strange – Alexey Ten Apr 28 '15 at 06:03
  • 2
    *"But OP wants to pass number literals."* Well, that's just the most common way to create number values. I'm trying to explain the reason for why that's not possible. If leading zeroes were significant to the number system, there surely would be a way to represent them via number literals as well. Even if you are not using number literals, but strings converted to numbers, e.g. `Number("0005")`, you would not be able to "store" the leading zeros, indicating that not being able to use leading zeros in number literals is not a shortcoming of the number literal syntax itself. – Felix Kling Apr 28 '15 at 06:06
  • @istos "use strict", Luke :) – Alexey Ten Apr 28 '15 at 06:38
1

By default you wont get the leading numbers if you are storing it as an int. If you want to get the leading zeros then you need to send it as a string. Else it is not possible

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331