Reputation: 109
I'm Learning Javascript.
What does it mean to have parseFloat & parseInt functions in javascript, when in fact, Javascript does not differentiate between float and integer--there is only the Numeric data type.
Seems like they should have created parseNumber to be in alignment with their data types.
Any comments? Maybe somebody knows the reason. It just seems very odd.
Upvotes: 1
Views: 3278
Reputation: 3802
parseFloat()
and parseInt()
is javascript functions which is basically use to convert string into Float or Integer number
var a = parseInt("10");
var b = parseInt("10.00");
var c = parseInt("10.33");
var d = parseInt("34 45 66");
var e = parseInt(" 60 ");
var f = parseInt("40 years");
var g = parseInt("He was 40");
var h = parseFloat("10");
var i = parseFloat("10.00");
var j = parseFloat("34 45 66");
var k = parseFloat("10.33");
var l = parseFloat(" 60 ");
var m = parseFloat("40 years");
var n = parseFloat("He was 40");
console.log("a : " + a);
console.log("b : " + b);
console.log("c : " + c);
console.log("d : " + d);
console.log("e : " + e);
console.log("f : " + f);
console.log("g : " + g);
console.log("h : " + h);
console.log("i : " + i);
console.log("j : " + j);
console.log("k : " + k);
console.log("l : " + l);
console.log("m : " + m);
console.log("n : " + n);
Upvotes: 1
Reputation: 1337
These functions are basically to parse string data to the relative format so according to the conditions we can use them
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
The parseFloat() function parses an argument and returns a floating point number.
console.log(parseInt("1.11"))
console.log(parseFloat("1.11"))
Upvotes: 2
Reputation: 7899
In JS
, there exist Number
not parseNumber
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number. It works with numeric value, no matter whether it's float or int.
parseInt
or parseFloat
are generally used to parse string
to number.
In decimal number i:e 10.3 the parseInt
only return the number before decimal and discard everything after it, whereas with parseFloat
it does consider the digits after decimal.
const num = 10.3;
console.log(parseInt(num));
console.log(parseFloat(num));
Upvotes: 1
Reputation: 1565
parseInt("1.0") will be same as parseInt("1.1") parseFloat("1.0") will be different to parseFloat("1.1")
Upvotes: 2