What Is Special About Numbers Starting With Zero

What is special about numbers starting with zero?

Yes, this is expected.

[C++11: 2.14.2/1]: An integer literal is a sequence of digits that has no period or exponent part. An integer literal may have a prefix that specifies its base and a suffix that specifies its type. The lexically first digit of the sequence of digits is the most significant. A decimal integer literal (base ten) begins with a digit other than 0 and consists of a sequence of decimal digits. An octal integer literal (base eight) begins with the digit 0 and consists of a sequence of octal digits.22 A hexadecimal integer literal (base sixteen) begins with 0x or 0X and consists of a sequence of hexadecimal digits, which include the decimal digits and the letters a through f and A through F with decimal values ten through fifteen. [ Example: the number twelve can be written 12, 014, or 0XC. —end example ]

22 The digits 8 and 9 are not octal digits.

Javascript 0 in beginning of number

Leading 0 on a numerical literal indicates that an octal integer is the intention, unless the second digit is 8 or 9. In that case, the leading 0 is ignored.

Because octal numeric literals must be integers, 02.5 is erroneous.

This behavior was logged as a bug in Firefox in 2014, but because there's so much JavaScript code in the world and so much of it (probably inadvertently) relies on 09.3 not being a syntax error, the bug was marked "WONTFIX".

As pointed out in a comment below, in "strict" mode octal constants are disallowed entirely.

Why does adding a zero before a number change its value?

Adding a zero before a number tells javascript to interpret it as an octal (base 8) value.

According to the MDN documentation on number literals, you can use the following to represent numbers on different base:

  • decimal: numbers starting with any digit other than zero are decimal.
  • binary: numbers starting with 0b or 0B
  • octal: numbers starting with 0
  • hexadecimal starting with 0x or 0X`
  • exponentiation ending with e followed by a number (1e3 = 1x103)

PHP arithmetic operation where number starts with zero

As already specified by @Rizier123 you must follow the integer type doc.

As specified in the official documentation of integers type http://php.net/manual/en/language.types.integer.php

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +)

$n = 123; // decimal number
$n = -123; // negative number
$n = 0234; // octal number
...

So in your case the first number $a = 012 means that it's in octal notation.

When you do the division: $a/4 PHP proceed with type juggling converting it to decimal base and dividing by 4

$a = 012; // 10 in decimal base
echo $a/4; // 10/4 -> 2.5

Note: Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored

jquery issue with numbers starting wth zero

A number that starts with a zero is parsed as an octal number (base 8), not as a decimal number (base 10).

Specify the radix (base) when you parse the number:

parseInt($(this).attr('class'), 10)

Also, unless you only have dates within the same month, you should use an ISO 8601 based date format (ymd), e.g. 100924 rather than 240910.

With the date first, you get the effect that for example 250810 > 240910.

Use numbers starting with 0 in a variable in php

Use it as a String:

$number = '010';


Related Topics



Leave a reply



Submit