Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  35] [ 5]  / answers: 1 / hits: 15382  / 12 Years ago, fri, october 19, 2012, 12:00:00

Through my javascript library, I end up with a string that represents a number. Now I want to preform an addition on that number without it doing a string concatenation instead. The solution is easy (
How do I add an integer value with javascript (jquery) to a value that's returning a string?, How to make an addition instead of a concatenation) if your number is always in integer. However my string could be a float or an integer, and at the time of the addition, and I don't know which it will be. Is there a way to make sure the addition happens regardless of whether it's a float or integer?


More From » string

 Answers
12

Try using parseFloat. The input variables will be converted to floats, which will work whether the string contains an integer or a float. Your result will always be a float.


[#82467] Thursday, October 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;