I have a array like this.
var elements=[5614,6619,7220,7320,7830,8220,0111,0112,0113,0142,0149]
Am converting every element to string so as to use with jquery autocomplete.
Am using .map function to do this.
elements = elements.map(String);
output is
["5614", "6619", "7220", "7320", "7830", "8220", "73", "74", "75", "98", "149"]
Function is taking 0111,0112,0113,0142 all these values as Octal values and converting them to decimal.
I don't want this conversation and want to preserve leading Zero also , How can I do this , please help.
Function is taking 0111,0112,0113,0142 all these values as Octal values and converting them to decimal.
It's not the function doing that, it's this:
var elements=[5614,6619,7220,7320,7830,8220,0111,0112,0113,0142,0149]
In loose mode, if you start a number with a 0 followed by a series of octal digits, it's octal. That's why 010 === 8 is true:
console.log(010 === 8); // true
And heaven help us, but if you have a 0 followed by a number with non-octal decimal digits (8 or 9), it's decimal, which is why 011 === 09 and 9 === 09 are true:
console.log(011 === 09); // true
console.log(9 === 09); // true
The solution is:
Use strict mode ("use strict";). Both legacy octal literals (010) and legacy non-octal decimal literals (08) are disallowed in strict mode. (If you need to write octal, you can, with the newer 0o10 format — that's the number eight.)
Don't write leading zeros on numbers (with the possible exception of a 0 just prior to a . in a fractional number less than one)
You can't fix elements after the fact (because it's impossible to know, once they're numbers, which ones were incorrectly written in octal), you have to fix it at the point you're creating it, e.g.:
var elements=[5614,6619,7220,7320,7830,220,111,112,113,142,149]
Related
JavaScript supports different number bases. Bases 2, 8, 10 and 16 are easily written as:
Number(0b10) //2
Number(0o10) //8
Number(10) //10
Number(0x10) //16
Now I want to enter a 4 digit number from 0000-9999 the problem is that in JavaScript not only 0oXX is octal, but also numbers with leading 0's (if it can). Is there any way to determine the "real" base 10 original input, without using a string input?
Example:
Number(010) == Number(0o10) //would be true
There is now way to force JS to treat Number(010) as decimal.
When you write 010 - it's octal literal. But in strict mode 0-prefixed are not allowed.
"use strict"
010.toString()
//Uncaught SyntaxError: Octal literals are not allowed in strict mode.
"use strict"
0o10.toString()
"8"
So it seems to me, that you'll have to use text input and parse it to achieve your goal.
I want to evaluate number starts with zero as a decimal number.
For example, let's define convertToDec
convertToDec(010) => 10
convertToDec(0010) => 10
convertToDec(0123) => 123
etc..
Because all js numbers starts with 0 are evaluated in base 8, I tried to do it like this:
function convertToDec(num){
return parseInt(num.toString(), 10);
}
But the toString function parses the number in base 8.
Any suggestions?
Thanks!
If you literally write 0010 in JavaScript, then it will be treated as an octal number. That's just how the parser works.
From MDN's docs:
Decimal integer literal consists of a sequence of digits without a leading 0 (zero).
Leading 0 (zero) on an integer literal indicates it is in octal. Octal integers can include only the digits 0-7.
Leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F.
Leading 0b (or 0B) indicates binary. Binary integers can include digits only 0 and 1.
So, when you write convertToDec(0010), your browser interprets this as convertToDec(8). It's already been "converted" to an 8 since you used an "octal literal".
If you want the literal value "0010", then you'll need to use a string.
parseInt("0010", 10); // 10
You need to call convertToDec with string arguments, not numbers.
function convertToDec(num){
return parseInt(num, 10);
}
alert(convertToDec("010"));
If you give it a number as the argument, the number has already been parsed by the Javascript interpreter, the function can't get back what you originally typed. And the JS interpreter parses numbers beginning with 0 as octal.
I receive a long XML from backend. To further use the xml I convert it to JSON object using one of the standard XMLtoJSON javascript library. The issue is, some of the XML value contains number with leading zeros eg: 001072.
The problem is, when javascript library converts xml to JSON, number with leading zeros give completely different value.
For example
“001072” converts “570”
Other times it parse it correctly. For example:
“0045678” converts to 45678
The problem is how javascript handle number with zeros. I don’t know the reason of this strange behavior!!
Please suggest a solution which can parse number with zeros consistently and how can I use it with xmltojson library
This is most likely a problem with octal literals. If a number starts with a leading 0, JavaScript by default will try to parse it as an octal literal.
For this reason, you should always specify the radix parameter when calling parseInt. The library probably does not do that.
parseInt("012", 8); // 10
parseInt("012", 10); // 12
I think this is the offending line in the library, probably. Either edit the library, or edit your XML.
Octal numbers with the leading zero are on the way out. For ECMAScript5 they can still cause problems and are thus not allowed in strict mode and throw a runtime error. You really should not be using 3rd party scripts that are not in strict mode they are dangerous for way too many reasons, as you can see with the handling of the octal numbers.
As ECMAScript 6 becomes more wide spread the use of the leading zero will be pushed out all together.
Octals literals will have a '0o' prefix 0o10 === 8 can be uppercase 'o' but I am sure you can see this will be a hassle. ES6 will also formalise the binary format with the prefix 0b1000 === 8 though most browsers have supported it for some time. Hex has also been around for a while 0x08 == 8
The reason some numbers with leading zeros are decmil and some octal is dependent on what digits are in the number. Octal does not use the digits 8 and 9 so any number that have these digits can not be octal.
Can someone explain to me why when pass 1.0 to a function in javascript it gets converted to 1 and how to work around this quirk?
var return_me = function(value) {
return value;
}
console.log("1.0 is returned as " + return_me(1.0));
JavaScript doesn't distinguish between int or float like other more strongly typed languages. It just has one Number type. From the ECMA specifications:
Once the exact mathematical value (MV) for a numeric literal has been determined, it is
then rounded to a value of the Number type. If the MV is 0, then the
rounded value is +0; otherwise, the rounded value must be the Number
value for the MV (as specified in 8.5), unless the literal is a
DecimalLiteral and the literal has more than 20 significant digits, in
which case the Number value may be either the Number value for the MV
of a literal produced by replacing each significant digit after the
20th with a 0 digit or the Number value for the MV of a literal
produced by replacing each significant digit after the 20th with a 0
digit and then incrementing the literal at the 20th significant digit
position. A digit is significant if it is not part of an ExponentPart
and
it is not 0;
or there is a nonzero digit to its left and there is a nonzero digit, not in the ExponentPart, to its right.
A conforming
implementation, when processing strict mode code (see 10.1.1), must
not extend the syntax of NumericLiteral to include OctalIntegerLiteral
as described in B.1.1.
More info on Number.
So basically, the answer is that JavaScript will display numbers that look like integers as integers and numbers that look like floats as floats.
In javascript there are six build in types of values.
string
number
boolean
null and undefined
object
symbol
These are mentioned to the book You don't know JS which I find really useful in my effort to learn javascript.
As a result js sees the var value of your function as a typeof number and understands that 1.0 is the same as 1. (in case the 1.0 was 1.9 it returns 1.9 as expected).
Now if you want to keep these decimals (even if there are zero digits) you could pass the value as a string.
console.log("1.0 is returned as " + return_me("1.0"));
This question already has answers here:
Prefix zero changes output in number addition [duplicate]
(2 answers)
Closed 9 years ago.
I have encountered a very weird issue with my JavaScript program. I have fount that JavaScript for some reason changes 040000 into 16384! [Example] Does anyone know why JavaScript is doing this?
It's because in js, number literals prepended with 0 are considered octal (base 8)
For example
010 == 8
In your example 040000 is really 4*8*8*8*8 = 16384 because in octal each 0 in the right multiplies the value by 8.
EDIT: Bonus:
If the leading 0 is in a string representation, (for example, if it was introduced by the user), and you want to avoid converting to octal, specify the base (aka radix) with value 10 in the parseInt method call, like this
var number = parseInt("040000", 10); //number will be 40000 ;)
In recent browsers, the radix is 10 by default, but not in old browsers, so if you want maximum compatibility also, always specify the radix parameter (usually 10).
Cheers
Because javascript thinks its in OCTAL format
Explanation:-
Javascript (like most programming languages) allows us to work directly with both octal and hexadecimal numbers, all we need is a way to tell which number base we are using when we specify a number. To identify octal and hexadecimal numbers we add something to the front of numbers using those bases to indicate which base we are using. A leading 0 on the front of a number indicates that the number following is octal while a leading 0x indicates a hexadecimal number. The decimal number 18 can therefore also be represented as 022 (in octal) and 0x12 (in hexadecimal). We don't put a special symbol on the front of decimal numbers so any number that doesn't start with 0 or 0x is assumed to be decimal.
So its same in your case
040000(base8)=16384(base10)