5

It works with a lot number types, but not with negatives hexadecimal or binary. Too, Number(octal) doesn't parse an octal number.

Number("15")    ===   15;  // OK
Number("-15")   ===  -15;  // OK
Number("0x10")  ===   16;  // OK
Number("0b10")  ===    2;  // OK
Number("-0x10") ===  NaN;  // FAIL (expect  -16)
Number("-0b10") ===  NaN;  // FAIL (expect   -2)
Number("0777")  ===  777;  // FAIL (expect  511)
Number("-0777") === -777;  // FAIL (expect -511)

Question: how I can parse all valid Javascript numbers correctly?

Edit A

parseInt() don't help me because I need check by each possibility (if start with 0x I use 16, for instance).

Edit B

If I write on Chrome console 0777 it turns to 511, and too allow negative values. Even works if I write directly into javascript code. So I expect basically a parser that works like javascript parser. But I think that the negative hexadecimal, for instance, on really is 0 - Number(hex) in the parser, and not literraly Number(-hex). But octal values make not sense.

9
  • 1
    You could easily create your own function which inspects the first three characters of the string and handles it accordingly. Commented Mar 6, 2015 at 20:45
  • 5
    parseInt() with the matching radix seems to work. Commented Mar 6, 2015 at 20:45
  • 2
    parseInt("-0x10",16) gives you -16, and parseInt("-0777",8) gives you -511 Commented Mar 6, 2015 at 20:45
  • 1
    Binary, octal, and hexidecimal literals have not been defined to support signs. Only a DecimalLiteral includes the syntax for SignedInteger. Commented Mar 6, 2015 at 20:55
  • 4
    Regarding "Edit B:" Octal literals have changed in ES6 to 0o[0-7]+. The now "legacy" syntax of 0[0-7]+ only continues to be available when strict mode is not used. Commented Mar 6, 2015 at 21:15

4 Answers 4

3

Try this:

parseInt(string, base):

parseInt("-0777", 8) 
parseInt("-0x10", 16) 
Sign up to request clarification or add additional context in comments.

1 Comment

I guess the issue is that the base is not known beforehand.
2

You could write a function to handle the negative value.

function parseNumber (num) {
   var neg = num.search('-') > -1;
   var num = Number(num.replace('-', ''));
   return num * (neg ? -1 : 1);
}

Comments

0

It's not parsing octal and the other examples because they're not valid Javascript numbers, at least within the constraints of Number. So the technically correct answer is: use Number!

If you want to parse other formats, then you can use parseInt, but you will have to provide the base.

3 Comments

"because they're not valid Javascript numbers": Why is -0x10 not a valid number?
It's not valid within the constraints of Number. As a way-out example, π² is a valid number, but not within Number, or within Javascript!
If you refer to the algorithm that converts strings to numbers (which is different from the Number function!) (ecma-international.org/ecma-262/5.1/#sec-9.3.1), then I agree. When I said "valid number" I was meaning within JavaScript. So yes, π² is not a valid number but -0x10 is: console.log(-0x10); (though technically 0x10 is the number - is the unary minus operator). The point is that this character sequence is valid in JS source.
0

This gets a little ugly, but you could inspect the values to determine the right radix for parseInt. In particular, the b for binary doesn't seem to be support by my browser (Chrome) at all, so unlike the OP, Number("0b10") gives me NaN. So you need to remove the b for it to work at all.

var numbers = [
  "15", "-15", "0x10", "0b10", "-0x10", "-0b10", "0777", "-0777"
];

function parser(val) {
  if (val.indexOf("x") > 0) {
    // if we see an x we assume it's hex
    return parseInt(val, 16);
  } else if (val.indexOf("b") > 0) {
    // if we see a b we assume it's binary
    return parseInt(val.replace("b",""),2);
  } else if (val[0] === "0") {
    // if it has a leading 0, assume it's octal
    return parseInt(val, 8);
  }
  // anything else, we assume is decimal
  return parseInt(val, 10);
}

for (var i = 0; i < numbers.length; i++) {
  console.log(parser(numbers[i]));
}

Note this obviously isn't foolproof (for example, I'm checking for x but not X), but you can make it more robust if you need to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.