Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a currency string to a double with jQuery or JavaScript?
Converting currency strings to numerical values is essential in web applications for calculations and data processing. This article demonstrates how to convert currency strings to doubles using JavaScript methods.
There are two popular approaches to convert currency strings into numerical values using JavaScript built-in methods. We'll explore both methods with practical examples.
Method 1: Using substring() and charCodeAt()
This approach uses a loop to iterate through each character, checking if it's numeric using Unicode values:
Use the
charCodeAt()method to get the Unicode value of each characterUse the
substring()method to extract the numeric portion from the original stringUse the
parseFloat()method to convert the extracted string to a double value
Key Methods
The substring() method ? Extracts a portion of a string between start and end indices:
string.substring(startIndex, endIndex)
The parseFloat() method ? Converts a string to a floating-point number:
parseFloat(value)
The charCodeAt() method ? Returns the Unicode value of the character at a specified index:
str.charCodeAt(index)
Example
Here's how to convert a currency string to a double using character checking:
<!DOCTYPE html>
<html>
<body>
<h1>Converting Currency String To Double</h1>
<script>
function convert(currency) {
var k, temp;
for(var i = 0; i < currency.length; i++) {
k = currency.charCodeAt(i);
if(k > 47 && k < 58) { // Check if character is digit (0-9)
temp = currency.substring(i);
break;
}
}
temp = temp.replace(/,/g, ''); // Remove commas
return parseFloat(temp);
}
var string_currency = "$2,245.45";
document.write("Currency value: " + string_currency + "<br>");
var doubleValue = convert(string_currency);
document.write("Double value: " + doubleValue + "<br>");
</script>
</body>
</html>
Currency value: $2,245.45 Double value: 2245.45
Method 2: Using replace() with Regular Expression (Recommended)
The replace() method with a regular expression is more efficient and concise. It removes all non-numeric characters except digits, dots, and minus signs:
Syntax
str.replace(searchValue, replaceValue)
Example
Using regular expression to clean the currency string:
<!DOCTYPE html>
<html>
<body>
<h1>Converting Currency String To Double</h1>
<script>
function convert(currency) {
// Remove all non-numeric characters except digits, dots, and minus
var temp = currency.replace(/[^0-9.-]+/g, "");
return parseFloat(temp);
}
var string_currency = "$800,868.51";
document.write("Currency value: " + string_currency + "<br>");
var doubleValue = convert(string_currency);
document.write("Converted to double: " + doubleValue + "<br>");
// Additional examples
document.write("<br>More examples:<br>");
document.write("?1,234.56 = " + convert("?1,234.56") + "<br>");
document.write("¥-999.99 = " + convert("¥-999.99") + "<br>");
</script>
</body>
</html>
Currency value: $800,868.51 Converted to double: 800868.51 More examples: ?1,234.56 = 1234.56 ¥-999.99 = -999.99
Comparison
| Method | Code Complexity | Performance | Handles Negatives |
|---|---|---|---|
| substring() + charCodeAt() | High | Slower | No |
| replace() + RegExp | Low | Faster | Yes |
Conclusion
The replace() method with regular expressions is the recommended approach for converting currency strings to doubles. It's more concise, handles negative values, and performs better than manual character parsing.
