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.
Related
This question already has answers here:
How to output numbers with leading zeros in JavaScript? [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to convert a string to number, which is "09"
parseInt('09') //returns 9 always
What is the best way to get the number with leading zero and it would be better if someone explain, why the designed parseInt in such a way that doesn't care about leading zeros.
An int cannot have leading zeroes, since they don't have any mathematical meaning.
To store a number, languages use a binary representation (don't forget everything is 0s and 1s in the end). This representation is the same for 9, 09, or 00009. When the number must be printed, it is converted back to a string representation, so you lose the leading zeroes.
If you need to store/remember the 0s, your only choice is to store the string representation of the number.
What you could do is storing both the number and string representation, like this:
function MyInt(s){
this.asString = s;
this.num = parseInt(s);
}
var i = new MyInt("09");
console.log(i.num); // 9
console.log(i.asString); // 09
Take note that leading 0 don't have any value for int. If want to use for display purpose use string and for calculation part use parseInt(someval)
parseInt takes two parameters: string and radix.
radix tells parseInt how to convert a string to a number.
If you leave the radix out, the rules are somewhat simple for how it decides what radix to use:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
If the input string begins with "0", radix is eight (octal) or 10 (decimal).
Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
That said, you are using a function whose non-abbreviated name is "Parse Integer." You cannot make it retain leading zeros, because leading zeros have no mathematical value.
I have already tried the following:
discval = 2.833423
discval = discval.toFixed(2).toString().replace("." , ",");
discval = parseFloat(discval);
The output is 2 and not 2,83
Any idea?
parseFloat("2,83") will return 2 because , is not recognized as decimal separator, while . is.
If you want to round the number to 2 decimal places just use parseFloat(discval.toFixed(2)) or Math.round(discval * 100) / 100;
If you need this jut for display purposes, then leave it as a string with a comma. You can also use Number.toLocaleString() to format numbers for display purposes. But you won't be able to use it in further calculations.
BTW .toFixed() returns a string, so no need to use .toString() after that.
From https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
parseFloat parses its argument, a string, and returns a floating point
number. If it encounters a character other than a sign (+ or -),
numeral (0-9), a decimal point, or an exponent, it returns the value
up to that point and ignores that character and all succeeding
characters. Leading and trailing spaces are allowed.
If the first character cannot be converted to a number, parseFloat
returns NaN.
, is not an expected character so the number is truncated to that.
It's not possible to change the representation of floating point numbers in Javascript, you will need to treat your number as a string if you want to separate decimals with a comma instead of dot.
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"));
I have just observed that the parseInt function doesn't take care about the decimals in case of integers (numbers containing the e character).
Let's take an example: -3.67394039744206e-15
> parseInt(-3.67394039744206e-15)
-3
> -3.67394039744206e-15.toFixed(19)
-3.6739e-15
> -3.67394039744206e-15.toFixed(2)
-0
> Math.round(-3.67394039744206e-15)
0
I expected that the parseInt will also return 0. What's going on at lower level? Why does parseInt return 3 in this case (some snippets from the source code would be appreciated)?
In this example I'm using node v0.12.1, but I expect same to happen in browser and other JavaScript engines.
I think the reason is parseInt converts the passed value to string by calling ToString which will return "-3.67394039744206e-15", then parses it so it will consider -3 and will return it.
The mdn documentation
The parseInt function converts its first argument to a string, parses
it, and returns an integer or NaN
parseInt(-3.67394039744206e-15) === -3
The parseInt function expects a string as the first argument. JavaScript will call toString method behind the scene if the argument is not a string. So the expression is evaluated as follows:
(-3.67394039744206e-15).toString()
// "-3.67394039744206e-15"
parseInt("-3.67394039744206e-15")
// -3
-3.67394039744206e-15.toFixed(19) === -3.6739e-15
This expression is parsed as:
Unary - operator
The number literal 3.67394039744206e-15
.toFixed() -- property accessor, property name and function invocation
The way number literals are parsed is described here. Interestingly, +/- are not part of the number literal. So we have:
// property accessor has higher precedence than unary - operator
3.67394039744206e-15.toFixed(19)
// "0.0000000000000036739"
-"0.0000000000000036739"
// -3.6739e-15
Likewise for -3.67394039744206e-15.toFixed(2):
3.67394039744206e-15.toFixed(2)
// "0.00"
-"0.00"
// -0
If the parsed string (stripped of +/- sign) contains any character that is not a radix digit (10 in your case), then a substring is created containing all the other characters before such character discarding those unrecognized characters.
In the case of -3.67394039744206e-15, the conversion starts and the radix is determined as base 10 -> The conversion happens till it encounters '.' which is not a valid character in base 10 - Thus, effectively, the conversion happens for 3 which gives the value 3 and then the sign is applied, thus -3.
For implementation logic - http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
More Examples -
alert(parseInt("2711e2", 16));
alert(parseInt("2711e2", 10));
TO note:
The radix starts out at base 10.
If the first character is a '0', it switches to base 8.
If the next character is an 'x', it switches to base 16.
It tries to parse strings to integers. My suspicion is that your floats are first getting casted to strings. Then rather than parsing the whole value then rounding, it uses a character by character parsing function and will stop when it gets to the first decimal point ignoring any decimal places or exponents.
Some examples here http://www.w3schools.com/jsref/jsref_parseint.asp
parseInt has the purpose of parsing a string and not a number:
The parseInt() function parses a string argument and returns an
integer of the specified radix (the base in mathematical numeral
systems).
And parseInt calls the function ToString wherein all the non numerical characters are ignored.
You can use Math.round, which also parses strings, and rounds a number to the nearest integer:
Math.round("12.2e-2") === 0 //true
Math.round("12.2e-2") may round up or down based on the value. Hence may cause issues.
new Number("3.2343e-10").toFixed(0) may solve the issue.
Looks like you try to calculate using parseFloat, this will give you the correct answer.
parseInt as it says, returns an integer, whereas parseFloat returns a floating-point number or exponential number:
parseInt(-3.67394039744206e-15) = -3
parseFloat(-3.67394039744206e-15) = -3.67394039744206e-15
console.log('parseInt(-3.67394039744206e-15) = ' , parseInt(-3.67394039744206e-15));
console.log('parseFloat(-3.67394039744206e-15) = ',parseFloat(-3.67394039744206e-15));
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)