Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  65] [ 4]  / answers: 1 / hits: 17755  / 9 Years ago, wed, august 5, 2015, 12:00:00

I'm trying to get rid of currency sign in order to calculate new price using parseFloat, but it returns nan for some reason.



var pricePerUser = £19.99;
pricePerUser = parseFloat(pricePerUser) * 3;
console.log(pricePerUser); //Returns NaN

More From » jquery

 Answers
21

parseFloat() will parse each character of a string until it finds a non-numeric value. The £ is the first character, hence it finds no number to parse. You need to remove that before you call parseFloat():


var pricePerUser = "£19.99";
pricePerUser = parseFloat(pricePerUser.replace('£', '')) * 3;
console.log(pricePerUser);


Could you suggest how I can print out numbers like 19.90 instead of 19.9 in this case?



If you require 2DP precision use toFixed(2) when displaying the output to the UI:


console.log(pricePerUser.toFixed(2));

[#65533] Monday, August 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rafael

Total Points: 5
Total Questions: 103
Total Answers: 103

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;