i wrote from 001 to 0010 and much more digit like this that started with "00" in chrome console and Fire Fox even in IE and get this result.
why 0010 is not equal to 10 ?
or why 0020 is not equal to 20 ? and it is "16".
A leading zero indicates that a number should be interpreted as octal.
Thus 10 interpreted as octal is equal to 8 in decimal.
For more information refer to MDN on number literals.
"Numeric constants are considered octal if they are preceded by a zero, and are considered hexadecimal if they are preceded by a zero and and x (0x)." (as explained here)
008 is not considered octal because it contains "8" which is not an octal number. 0010 is in fact an octal number and equals 8.
Number literals in Javascript can be entered in different bases -
a leading zero means the number is the number is in octal base (only digits 0-7) so 010 is the same as: one times 8 + zero
the literal 0x10 is in hexadecimal (base 16) so equals to: one times 16 + zero) = 16
see here https://developer.mozilla.org/en/docs/JavaScript/Guide/Values,_variables,_and_literals
Because the leading 0 represents an Octal number system. Likewise, if you had typed 0x010 it would equal to 16, since 0x is prefix for Hexadecimal number system.
Related
Since integers above 2^53 can't be accurately represented in doubles, how does JS decide on their decimal representation when they are printed as strings?
For example, 2^55 is 36028797018963968, and printf("%lf",(double)(1LL<<55)) in C will print that number correctly, since it has trailing zeroes in its binary representation that do not cause precision loss when truncated.
However, in Javascript, we get 36028797018963970 instead. It seems to try to round numbers to get a 0 at the end, but not always - for instance, 2^55-4 is represented correctly with 4 at the end.
Is there some place in the spec that defines this weird behavior?
Question- Since integers above 2^53 can't be accurately represented in doubles, how does JS decide on their decimal representation when they are printed as strings?
1. Way of Printing decimal numbers in JS
JavaScript numbers are internally stored in binary floating point and usually displayed in the decimal system.
There are two decimal notations used by JavaScript:
Fixed notation
[ "+" | "-" ] digit+ [ "." digit+ ]
Exponential notation
[ "+" | "-" ] digit [ "." digit+ ] "e" [ "+" | "-" ] digit+
An example of exponential notation is 1.2345678901234568e+21.
Rules for Displaying decimal numbers:
A. Use exponential notation if there are more than 21 digits before the decimal point.
B. Use exponential notation if the number starts with “0.” followed by more than five zeros.
2. The ECMAScript 5.1 display algorithm
Here is a details of Sect. 9.8.1 of the ECMAScript 5.1 specification describes the algorithm for displaying a decimal number
Given a number
mantissa × 10^pointPos−digitCount
The mantissa of a floating point number is an integer – the significant digits plus a sign. Leading and trailing zeros are discarded. Examples:
The mantissa of 12.34 is 1234.
Case-1. No decimal point: digitCount ≤ pointPos ≤ 21
Print the digits (without leading zeros), followed by pointPos−digitCount zeros.
Case-2. Decimal point inside the mantissa: 0 < pointPos ≤ 21, pointPos < digitCount
Display the pointPos first digits of the mantissa, a point and then the remaining digitCount−pointPos digits.
Case-3. Decimal point comes before the mantissa: −6 < pointPos ≤ 0
Display a 0 followed by a point, −pointPos zeros and the mantissa.
Case-4. Exponential notation: pointPos ≤ -6 or pointPos > 21
Display the first digit of the mantissa. If there are more digits then display a point and the remaining digits. Next, display the character e and a plus or minus sign (depending on the sign of pointPos−1), followed by the absolute value of pointPos−1. Therefore, the result looks as follows.
mantissa0 [ "." mantissa1..digitCount ]
"e" signChar(pointPos−1) abs(pointPos−1)
Question-
However, in Javascript, we get 36028797018963970 instead. It seems to try to round numbers to get a 0 at the end, but not always - for instance, 2^55-4 is represented correctly with 4 at the end.
Is there some place in the spec that defines this weird behavior?
Check: How numbers are encoded in JavaScript specially ==>5. The maximum integer
Additional Reference: https://medium.com/dailyjs/javascripts-number-type-8d59199db1b6
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.
If you divide 11/10 in javascript you get 1.1. If you divide 011/10 in javascript you get 0.9. What causes the difference in results?
011 is an octal constant equal to 9.
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Integers:
Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8).
Leading 0 (zero) on an integer literal indicates it is in octal. Octal integers can include only the digits 0-7.
Also note:
Octal integer literals are deprecated and have been removed from the ECMA-262, Edition 3 standard (in strict mode).
C++ also has octal constants.
If the number you are dealing with is represented as a string, you can specify the radix you want when casting it to a number by using parseInt().
+'011' // 9
parseInt('011') // 9
parseInt('011', 10) // 11
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)
i have few numbers like
0011,0101,0123,1234,5245,0052,3265,0047,0124
How replace prefix zero only,
like no number should start with zero ,
exactly like
0011 should be 11 ,
0101 should be 101 ,
0123 should be 123
How to do this ?
Is ther any javascript function there ,
Thanks
parseInt("0011", 10);
will return a Number with the value of 11. Note that the second argument is important, otherwise your zero-prefixed numbers would be interpreted as octal.