Javascript why it is treated as octal

I pass the identifier of the javascript function as a parameter, because it comes from the user interface, it is padded with zeros. but it seems to have (maybe) "weird" behavior?

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

How can I make sure they are parsed into the correct numbers? (decimal)

EDIT:

just make it clearer:

these numbers come from the server and are strings padded with zeros. and I make a delete button for each.

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 did my best:

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

and still understand how octal

+4
source share
6 answers

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

  parseInt(string, 10) 

interpret strings as decimal even if they begin with 0.

In your test, you pass the parseInt method a number, not a string, which is probably why it does not return the expected result.

Try

  parseInt('0000022115', 10) 

instead

 parseInt(0000022115, 10) 

which returns 221115 for me.

+5
source

If you start with 0, it is interpreted as an octal number.

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

Pay attention to the warning article:

You should never precede a number with zero, unless you are in particular for octal conversion!

Think About How To Look For Ideas To Delete 0s Chapters: Trim Leading Zeros Of A Line In Javascript

+4
source

A leading 0 indicates that the number is octal.

parseInt parses a string containing a number.
parseInt(0000022115, 10) passes a numeric literal. The literal is parsed in the JS octal interpreter, so you pass the original numeric value to parseInt .

+1
source

If you cannot intercept the line version of this number, you are out of luck.

At the same time, if you can get a lowercase version of your octal (calling toString() will not help), this will work:

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

Try

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

This should only give you numbers stripping zeros (if you need to support decimals, you will need to edit the account โ€œ.โ€, And of course โ€œalso funโ€ ... and I really hope you don't have to handle all the crazy ones different ways that Europeans write numbers!)

0
source

If the number came from the server as a string padded with zeros, use +"0000022115"

 console.log(+"0000022115") if (021 < 019) console.log('Paradox'); 

JS treats numbers padded with zeros as octal only if they are real octal numbers - if not, then it treats them as decimal. To discover the paradox of 'use strict' mode

 'use strict' if (021 < 019) console.log('Paradox'); 
0
source

Source: https://habr.com/ru/post/1494523/


All Articles