What you will learn in this post
parseFloatare three ways to convert to
Sometimes we think someone or something is strange only because we expect differently.
String. Other than that is the
Object type, which is called the reference type.
Primitive values are simple pieces of data that are stored on the stack, which is to say that their value is stored directly in the location that the variable accesses. Reference values, on the other hand, are objects that are stored in the heap, meaning that the value stored in the variable location is a pointer to a location in memory where the object is stored.
When you assign a variable to be primitive type, the value of the variable on the right side is copied. As for reference type, the reference of the variable on the right side is assign to left one.
You may be suprised at
String is created, it cannot be changed anymore.
In the above code, when executing the second line, the previous value
"Hello, " and the new value
"World!" are used to create a new
String object and make
a reference the concatenated result.
Number() is the constructor of
Number type. With
Number("2.3"), we can get a number
parseInt() converts input to an integer, while
parseFloat() to a float number.
+operator does exactly as
Number()function performs conversions based on these rules:
- When applied to
falseget converted into
- When applied to numbers, the value is simply passed through and returned.
- When applied to
- When applied to
- When applied to strings, the following rules are applied:
- If the string contains only numbers, optionally preceded by a plus or minus sign, it is always converted to a decimal number, so
11(note: leading zeros are ignored).
- If the string contains a valid floating-point format, such as
"1.1", it is converted into the appropriate floating-point numeric value (once again, leading zeros are ignored).
- If the string contains a valid hexadecimal format, such as
"0xf", it is converted into an integer that matches the hexadecimal value.
- If the string is empty (contains no characters), it is converted to
- If the string contains anything other than these previous formats, it is converted into
- When applied to objects, the
valueOf()method is called and the returned value is converted based on the previously described rules. If that conversion results in
toString()method is called and the rules for converting strings are applied.
parseInt()function examines the string much more closely to see if it matches a number pattern.
- Leading white space in the string is ignored until the first non–white space character is found.
- If this first character isn’t a number, the minus sign, or the plus sign,
NaN, which means the empty string returns
Number(), which returns
- If the first character is a number, plus, or minus, then the conversion goes on to the second character and continues on until either the end of the string is reached or a nonnumeric character is found. For instance,
"1234blue"is converted to
"blue"is completely ignored. Similarly,
"22.5"will be converted to
22because the decimal is not a valid integer character.
- Assuming that the first character in the string is a number, the
parseInt()function also recognizes the various integer formats (decimal, octal, and hexadecimal, as discussed previously). This means when the string begins with
"0x", it is interpreted as a hexadecimal integer; if it begins with
"0"followed by a number, it is interpreted as an octal value.
parseFloat()function works in a similar way to
parseInt(), looking at each character starting in position
0. It also continues to parse the string until it reaches either the end of the string or a character that is invalid in a floating-point number. This means that a decimal point is valid the first time it appears, but a second decimal point is invalid and the rest of the string is ignored, resulting in
"22.34.5"being converted to
Another difference in
parseFloat()is that initial zeros are always ignored. This function will recognize any of the floating-point formats discussed earlier, as well as the decimal format (leading zeros are always ignored). Hexadecimal numbers always become
parseFloat()parses only decimal values, there is no radix mode.
A final note: if the string represents a whole number (no decimal point or only a zero after the decimal point),
parseFloat()returns an integer.
OK, I doubt you’ve read that word by word. :stuck_out_tongue:
Well, you don’t have to, because I’m going to explain those confusing rules.
Converting string to number is always the most confusing part for all
parseFloat() methods. Although we can infer that the results of passing
"2.3" to those functions would be
2.3 respectively, we may probably be wrong when things get complicated.
First of all, we want to know when they will return a float number.
It’s clear that
parseInt() always returns an integer if input is legal while float number for
parseFloat(). But what about
There are characters which are not expected in a number (e.g.:
"t" are never expect, while
"e" may be part of a scientific notion (but not
parseFloat() judge them differently and may result in
NaN (stands for Not a Number) or just ignore them. So, let’s try them out one by one.
9100. But unfortunately,
parseInt() surprised us with the output of
parseInt() always returns an integer if the input is legal (in the way it thinks). So a simple implementation is to check from the first character whether is a number. If true, loop until the last character which is a number. So
It won’t check if there’s
"e" for scientific notion. This is another example of Worse is Better, which argues It is more important for the implementation to be simple than the interface. This makes sense to me. After all, you can still use
parseFloat() in this case.
parseFloat()are similar in that they both ignore from the first illegal character. But
"e"is illegal to
parseInt()but legal to
This may seems a little confusing, but I believe this is because scientific notion is mostly used as float rather than integer.
Now, I’d like to test the case when
"e" is in an illegal position (as defined in scientific notion).
"e" is at the first position, all of them give
NaN, which make sense as they’re illegal, although the whole story is more complicated than you might think.
"e" is at the last position, however,
3. It’s easy to understand that of
parseInt(), since we’ve discussed above. But it may surprise you a little that
parseFloat() also returns
3. In this case,
parseFloat() is different from
Number() in their judging method.
parseFloat() checks until the first illegal (which does not include
"e") or last character, while the last
"e" falls in
In this way, all results of
"3e" are the same as that of
Decimal point (
.) is legal to
parseFloat(), but illegal to
parseInt(). Since there’s no legal character before an illegal one,
If you think this is strange, consider the following case:
There’s nothing magic here. But if you think twice, you would probably find
parseInt()’s judging algorithm is efficient since it just ignore from the starting illegal character.
Now, what if we abuse decimal point?
Just bear it in mind that
parseFloat() tends to ignore from the first illegal character while
Numbur() checks them completely.
Other illegal characters include
"?" and so on.
I believe you are more confident now.
"0x11" is recognized as hexadecimal by
parseInt(), but as decimal by
parseFloat() (the leading zero is ignored).
"011" is not recognized as octal in all cases.
There is a discrepancy between ECMAScript 3 and 5 in regard to using parseInt() with a string that looks like an octal literal. For example:
// 56 (octal) in ECMAScript 3, 0 (decimal) in ECMAScript 5 var num = parseInt("070");
parseInt(), you can set the radix explicitly.
Number() supports hexadecimal but not octal;
parseInt() recognize in hexadecimal be default but can set the radix explicitly;
parseFloat() only support decimal form.
Number() checks the whole string so it’s more strict than the other two methods.
parseFloat() stop checking and ignore from the first illegal character, although they may have different judgment on whether a character is legal.
parseFloat() supports scientific notion while
parseInt() enables you to set radix explicitly.
There’s no remarkable advantage over the choice of
parseFloat(). You’d better have more information of your input and output form and then, choose a more suitable one for your situation.
View All Posts