function stringToFloat(str) { var fractional_part, integer, fractional = "", found = false, i; // remove leading and trailing non-digit symbols str = str.replace(/[^\d,\.]+$/, '').replace(/^[^\d,\.]+/, ''); fractional_part = str.slice(-3); for (i = fractional_part.length - 1; i >= 0; i--) { if (/\d/.test(fractional_part[i])) { fractional = fractional_part[i] + fractional; } else { found = true; break; } } if (fractional && found) { integer = str.slice(0, i - 3); } else { integer = str; fractional = ""; } return parseFloat(integer.replace(/[^\d]/g, '') + "." + fractional); }
Friday, August 2, 2013
Parse any number string to float
Labels:
javascript
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.