Javascript, why treated as octal

Related searches

I'm passing as parameter an id to a javascript function, because it comes from UI, it's left zero padded. but it seems to have (maybe) "strange" behaviour?

    console.log(0000020948);  //20948
    console.log(0000022115);   //9293 which is 22115's octal 
    console.log(parseInt(0000022115, 10));  // 9293 which is 22115's octal
    console.log(0000033959);  //33959
    console.log(20948);  //20948
    console.log(22115); //22115
    console.log(33959); //33959

how can I make sure they are parsing to right numebr they are? (decimal)

EDIT:

just make it clearer:

those numbers come from the server and are zero padded strings. and I'm making a delete button for each one.

like:

function printDelButton(value){
          console.log(typeof value);  //output string 
  return '<a href="#" onclick="deleteme('+value+')"><img src="images/del.png"></a>'
}

and 

function printDelButton(value){
console.log(typeof value); //output numeric
    console.log(value);   //here output as octal .... :S
 }

I tried :

console.log(parseInt(0000022115, 10));  // 9293 which is 22115's octal

and still parsing as Octal

If you receive your parameters as string objects, it should work to use

 parseInt(string, 10)

to interpret strings as decimal, even if they are beginning with 0.

In your test, you pass the parseInt method a number, not a string, maybe that's why it doesn't return the expected result.

Try

 parseInt('0000022115', 10)

instead of

parseInt(0000022115, 10)

that does return 221115 for me.

Why do so many languages treat numbers starting with 0 as octal , JavaScript is a client-side language, octal seems pretty useless. All three are pretty modern in other sense, and I don't think that there would be� JavaScript execution won't be halted. Cause of Error: This happens when any of the digits after the leading 0 is equal or greater than 8. This number cannot be treated as an octal number and therefore JavaScript gives a warning about it.

If you start it with a 0, it's interpreted as an Octal number.

See http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference#quickIDX2

Note the article's warning here:

You should never precede a number with a zero unless you are specifically looking for an octal conversion!

Consider looking here for ideas on removing the leadings 0s: Truncate leading zeros of a string in Javascript

Strange octal behavior, So without strict mode, I can get things like 022 === 018 (true), because 022 is interpreted as octal, and presumably 018 is treated as decimal since it can't be� Many languages treat numbers preceding with a 0 as octal, so the literal 010 is actually 8. A few among these is JavaScript, Python (2.7), and Ruby. But I don't really see why these languages need octal, especially when the more likely use of the notation is to denote a decimal number with a superfluous 0.

Leading 0s indicate that the number is octal.

parseInt parses a string containing a number. parseInt(0000022115, 10) passes a numeric literal. The literal is parsed in octal by the JS interpreter, so you're passing a raw numeric value to parseInt.

Preventing octal conversion - javascript, But it seems javascript converts the number into octal before I can do Leading and trailing spaces are allowed. so 1/2 treated as string. and it parse only 1. Hex, binary and octal numbers. Hexadecimal numbers are widely used in JavaScript to represent colors, encode characters, and for many other things. So naturally, there exists a shorter way to write them: 0x and then the number. For instance:

Unless you can intercept a string version of this number, you're out of luck.

That being said, if you can get a string version of your octal (calling toString() won't help), this will work:

parseInt(variable_string.replace(/^0+/, ''), 10);

farskid/better-js-integers: Auto fixing javascript flaw on octal , A better way to ensafe javascript integers from being treated as octal numbers. Javascript flaw on integers base, dealing with octal numbers (Out of strict mode):. This happens because an integer digit preceded by 0 is interpreted as an octal number. An octal number for decimal value 12 is 10. But what if x equals 092 well then the compiler will show an error because 92 is not a valid octal number.

Try

/^[0]*([1-9]\d)/.exec(numberFromUI)[0]

That should give you just the numbers stripping the zeros (if you have to support decimals, you'll need to edit to account for the '.', and of course ',' is fun too... and I really hope you don't have to handle all the crazy different ways Europeans write numbers! )

parseInt(), The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal� If radix is undefined, 0, or unspecified, JavaScript assumes the following: If the input string begins with "0x" or "0X" (a zero, followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexidecimal number. If the input string begins with "0" (a zero), radix is assumed to be 8 (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent.

In this example, since 8 is an invalid digit for representing the octal number, JavaScript ignores 0 prefix and treats the whole number as a decimal with the value 58. Note you can use the octal literals in non-strict mode. If you use them in strict mode, JavaScript will throw an error.

I’ve always wondered why leading zeroes (0) are used to represent octal numbers, instead of — for example — 0o. The use of 0o would be just as helpful, but would not cause as many problems … Tags history , language-agnostic , octal

A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease. no-octal - Rules - ESLint - Pluggable JavaScript linter

Comments
  • Instead of padding your numbers using zeros, use spaces.
  • Why does the UI left pad it with 0s?
  • It may be a good idea to use a "use strict" directive in your script. That will cause any use of octal literals to throw an error and it should make things easier to debug!
  • Regarding parseInt(0000022115, 10): You are still passing a number as first parameter, which makes JavaScript convert the number first and then pass it to parseInt. Pass a string instead.
  • If you're creating a new element from the '<a...</a>' string, then you need to wrap the value part of the string in quotes. '<a href="#" onclick="deleteme(\''+value+'\')">'
  • I updated the question. they are zero padded string came from the server.
  • @Kossel - so remove the zeroes from the string in JavaScript.
  • No; just use parseInt(str, 10) or parseFloat. The problem is that he has a literal.