javascript - parseFloat returns NaN -
i'm trying rid of currency sign in order calculate new price using parsefloat, returns nan reason.
var priceperuser = "£19.99"; priceperuser = parsefloat(priceperuser) * 3; console.log(priceperuser); //returns nan
parsefloat()
work on string until finds non-numeric value. £
first character, hence there no number parse. need remove before call parsefloat()
:
var priceperuser = "£19.99"; priceperuser = parsefloat(priceperuser.replace('£', '')) * 3; console.log(priceperuser);
if require 2dp precision, use tofixed(2)
:
console.log(priceperuser.tofixed(2));
Comments
Post a Comment