Possible Duplicate:
Behavior difference between parseInt() and parseFloat()
var box = $('.box'),
fontSize = parseInt(box.css('font-size'), 10) + 5;
$('button').on('click', function() {
box.animate({fontSize: fontSize});
});
//..
var box = $('.box'),
fontSize = parseFloat(box.css('font-size'), 10) + 5;
$('button').on('click', function() {
box.animate({fontSize: fontSize})
});
what the difference between..
**fontSize = parseInt(box.css('font-size'), 10);**
**fontSize = parseFloat(box.css('font-size'), 10);**
and and why should put 10 as a context..Please Help?
parseInt is for converting a non integer number to an int and parseFloat is for converting a non float (with out a decimal) to a float (with a decimal). If your were to get input from a user and it comes in as a string you can use the parse method to convert it to a number that you can perform calculations on.
The parseFloat() function parses an argument (converting it to a string first if needed) and returns a floating point number.
parseFloat is a bit slower because it searches for first appearance of a number in a string, while the Number constuctor creates a new number instance from strings that contains numeric values with whitespace or that contains falsy values.
parseFloat( ) parseFloat() is quite similar to parseInt() , with two main differences. First, unlike parseInt() , parseFloat() does not take a radix as an argument. This means that string must represent a floating-point number in decimal form (radix 10), not octal (radix 8) or hexadecimal (radix 6).
JavaScript provides two methods for converting non-number primitives into numbers: parseInt() and parseFloat() . As you may have guessed, the former converts a value into an integer whereas the latter converts a value into a floating-point number.
Any number literal contained in a string is also converted correctly, so the string "0xA" is properly converted into the number 10. However, the string "22.5" will be converted to 22 , because the decimal point is an invalid character for an integer. Some examples:
var iNum1 = parseInt("1234blue"); //returns 1234
var iNum2 = parseInt("0xA"); //returns 10
var iNum3 = parseInt("22.5"); //returns 22
var iNum4 = parseInt("blue"); //returns NaN
The parseInt() method also has a radix mode, allowing you to convert strings in binary, octal, hexadecimal, or any other base into an integer. The radix is specified as a second argument to parseInt() , so a call to parse a hexadecimal value looks like this:
var iNum1 = parseInt("AF", 16); //returns 175
Of course, this can also be done for binary, octal, and even decimal (which is the default mode):
var iNum1 = parseInt("10", 2); //returns 2
var iNum2 = parseInt("10", 8); //returns 8
var iNum2 = parseInt("10", 10); //returns 10
If decimal numbers contain a leading zero, it’s always best to specify the radix as 10 so that you won’t accidentally end up with an octal value. For example:
var iNum1 = parseInt("010"); //returns 8
var iNum2 = parseInt("010", 8); //returns 8
var iNum3 = parseInt("010", 10); //returns 10
In this code, both lines are parsing the string "010" into a number. The first line thinks that the string is an octal value and parses it the same way as the second line (which specifies the radix as 8). The last line specifies a radix of 10, so iNum3 ends up equal to 10.
Another difference when using parseFloat() is that the string must represent a floating-point number in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number 0908 will be parsed into 908 , and the hexadecimal number 0xA will return NaN because x isn’t a valid character for a floating-point number. There is also no radix mode for parseFloat() .
Some examples of using parseFloat() :
var fNum1 = parseFloat("1234blue"); //returns 1234
var fNum2 = parseFloat("0xA"); //returns 0
var fNum3 = parseFloat("22.5"); //returns 22.5
var fNum4 = parseFloat("22.34.5"); //returns 22.34
var fNum5 = parseFloat("0908"); //returns 908
var fNum6 = parseFloat("blue"); //returns NaN
Read More, Read More
Similar Question Read more here
First of all only parseInt
accepts second argument. It's called radix. It represents numeral system to be used. In example you can convert number into binary or hexadecimal code.
parseFloat
only accepts one argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With