1

I have a function that takes a number/integer as a parameter, and I'm trying to remove any possible leading zeros from that number before doing anything else in the function. However, numbers with leading zeros seem to be parsed in some way already before I can do something with the function parameter.

So far I tried this:

function numToString(num){
    var string = num.toString();
    console.log(string);
};

numToString(0011); // output is "9"
numToString(1111); // output is "1111"

I also tried this:

function numToString(num, base){
    var string = num.toString();
    var parse = parseInt(string, base);
    console.log(parse);
};

numToString(0011, 10); // output is "9"
numToString(1111, 10); // output is "1111"

Of course, the second example doesn't work since num.toString() didn't give my expected outcome in the first place. I don't want to replace the "num" function parameter with a string, but keep it as a number.

Maybe there is something obvious that I'm missing, but I can't figure quite out what it is. I am aware that a number with leading zeros is seen as an octal number, but I would like to know if I can work with the number that is entered as a parameter when it has leading zeros (i.e. it doesn't come from a variable, but is literally entered as a function parameter).

McVenco
  • 1,011
  • 1
  • 17
  • 30
  • 4
    If `num` is a number, then it can never have leading zeros – CertainPerformance Nov 30 '18 at 12:37
  • That crossed my mind, but how can I catch the given example of numToString(0011) then? Even if it is directly passed in the function somewhere in the code? – McVenco Nov 30 '18 at 12:43
  • octal numbers... – epascarello Nov 30 '18 at 12:44
  • Possible duplicate of [javascript returns 9 for 0011](https://stackoverflow.com/questions/27871143/javascript-returns-9-for-0011) – George Nov 30 '18 at 12:45
  • Possible duplicate of [Why JavaScript treats a number as octal if it has a leading zero](https://stackoverflow.com/questions/37003770/why-javascript-treats-a-number-as-octal-if-it-has-a-leading-zero) – Reinstate Monica Cellio Nov 30 '18 at 12:49
  • I'm aware of the number being seen as octal, but I was looking for a way to work with the actual input before it is even recognised as being an octal number. Say, I want to have this as an npm package (pure hypothetically), someone else installs it and enters `numToString(0011)` - is there even a way to catch that, or return an error when that happens? – McVenco Nov 30 '18 at 12:57
  • 1
    No - there is no way to "catch it" as it's already happened before your code ever sees the value. The *only* way to deal with it is for it to be a string. If you prefix a number with any number of zeros (and it's valid octal) it will be converted as if it is octal. When you type `numToString(0011)` the Javascript interpreter instantly sees `numToString(9)`. It is never `0011`. – Reinstate Monica Cellio Nov 30 '18 at 13:05
  • 1
    @Archer - "it's already happened before your code ever sees the value" I guess that is pretty much the explanation I was looking for. I think I will either use a string, or just document the function as "do NOT use leading zeros" :) . Thanks! – McVenco Nov 30 '18 at 13:13
  • 1
    _“I'm aware of the number being seen as octal, but I was looking for a way to work with the actual input before it is even recognised as being an octal number.“_ - then you would have to parse the JavaScript source code yourself … this isn’t possible in any other way. This is not _data_, it is _code_ (that then becomes data, but only later on) - what is happening here is happening at the _parser_ level. – misorude Nov 30 '18 at 13:22

1 Answers1

2

The reason this is happening is because leading a number with a zero makes javascript interpret the number in octal format. There is no way that you can change this interpretation, but if you're getting the value from somewhere as a string you could use parseInt(string, 10)

We can do this string conversion ourselves and then parse it in base 10 like so:

let number = 0011
console.log(parseInt(number.toString(8),10))

The other part of your question asks if you can throw when an octal number is entered.

'use strict' does just this:

Sixth, a strict mode in ECMAScript 5 forbids octal syntax. The octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and "\045" === "%". In ECMAScript 2015 Octal number is supported by prefixing a number with "0o". i.e. var a = 0o10; // ES2015: Octal

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

giraffesyo
  • 4,860
  • 1
  • 29
  • 39
  • The problem seems to be that the "num" parameter already is seen as an octal number before I can even do something with it. I was looking for a way to circumvent this and take the literally entered input and work with that. – McVenco Nov 30 '18 at 12:50
  • @McVenco Where are you passing it from? It had to have been made a number somewhere. – giraffesyo Nov 30 '18 at 12:51
  • See also my comment on the question: "Say, I want to have this as an npm package (pure hypothetically), someone else installs it and enters `numToString(0011)` - is there even a way to catch that, or return an error when that happens?" – McVenco Nov 30 '18 at 12:59
  • @McVenco Ok, see my edits. I think it's better to modify it where it's being saved because this way requires you to think about the type of the number in intermittent states, but this will serve your purposes. – giraffesyo Nov 30 '18 at 13:04
  • That helps, but it still would convert any entered number, with or without leading zeros. Maybe I would be better of using a string, or just document the function as "do NOT use leading zeros" :) – McVenco Nov 30 '18 at 13:13
  • Since numbers are stored in binary theres no way to *detect* if a number was entered in octal or not, its stored the exact same way, it's still just a number. It will indeed be up to the programmer to use it correctly. If you could get it as a string, you could write that detection logic yourself though. – giraffesyo Nov 30 '18 at 13:16
  • @McVenco Updated my answer again, there actually is a way to throw an error. – giraffesyo Nov 30 '18 at 13:20
  • Oh cool, that might indeed solve my problem! I'm not too familiar with strict mode so I didn't know that. I will accept your answer for that. – McVenco Nov 30 '18 at 13:40