In this article, we will convert a currency string to a double value with jQuery or Javascript. There are two popular ways to convert currency string into float string using different JavaScript inbuilt libraries.
Methods to convert Currency String to a Double value
- Using str.charCodeAt() and substring() methods
- Using JavaScript replace and parseFloat methods
Method 1: Using str.charCodeAt() and substring() method
This is a simple approach in which we will match characters one by one from starting using the loop and if any character comes out to be of integer type or numeric type then we will use the substring() method to take out the substring from the original string. After taking out the substring we will use the parseFloat() method to convert that string to float to a double value. To check whether the character is of numeric type or not, we use charCodeAt() method to get the Unicode value of the specified character.
The string.substring() is an inbuilt function in JavaScript which is used to return the part of the given string from the start index to the end index. Indexing starts from zero (0).
Syntax:
string.substring(Startindex, Endindex)
The parseFloat() is an inbuilt function in JavaScript which is used to accept the string and convert it into a floating point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number. It actually returns a floating point number parsed up to that point where it encounters a character that is not a Number.
Syntax:
parseFloat(value)
The str.charCodeAt() function returns a Unicode character set code unit of the character present at the index in the string specified as the argument.
Syntax:
str.charCodeAt(index)
The string.replace() is an inbuilt function in JavaScript which is used to replace a part of the given string with some another string or a regular expression. The original string will remain unchanged.
str.replace(A, B)
Example 1: This example uses the above-explained approach.
Javascript
// JavaScript script to convert // string currency to double // Function to convert function convert(currency) { let k, temp; // Loop to make substring for (let i = 0; i < currency.length; i++) { // Getting Unicode value k = currency.charCodeAt(i); // Checking whether the character // is of numeric type or not if (k > 47 && k < 58) { // Making substring temp = currency.substring(i); break ; } } // If currency is in format like // 458, 656.75 then we used replace // method to replace every ', ' with '' temp = temp.replace(/, /, "" ); // Converting string to float // or double and return return parseFloat(temp); } // Driver code // Currency in string let string_currency = "$450.45" ; console.log( "Currency value: " + string_currency); // Converting currency let doubleValue = convert(string_currency); // Display currency console.log( "Double value: " + doubleValue); // Currency in string string_currency = "$45, 645.45" ; console.log( "Currency value: " + string_currency); // Converting currency doubleValue = convert(string_currency); // Display currency console.log( "Double value: " + doubleValue); |
Currency value: $450.45 Double value: 450.45 Currency value: $45, 645.45 Double value: 45645.45
Method 2: Using JavaScript replace and parseFloat methods
In this method, we will use replace()method of JavaScript to replace every character other than numbers and decimal (.) with blank (“”). After then we can use parseFloat() method to make the string float or double. This method is more efficient than method 1.
The string.replace() is an inbuilt function in JavaScript which is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.
Syntax:
str.replace(A, B)
The parseFloat() is an inbuilt function in JavaScript which is used to accept the string and convert it into a floating point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number. It actually returns a floating point number parsed up to that point where it encounters a character that is not a Number.
Syntax:
parseFloat(value)
Example: This example shows the above-explained approach.
Javascript
// JavaScript script to convert // string currency to double // Function to convert function convert(currency) { // Using replace() method // to make currency string suitable // for parseFloat() to convert let temp = currency.replace(/[^0-9.-]+/g, "" ); // Converting string to float // or double and return return parseFloat(temp); } // Driver code // Currency in string let string_currency = "$6542.45" ; console.log( "Currency value: " + string_currency); // Converting currency let doubleValue = convert(string_currency); // Display currency console.log( "Converted to double: " + doubleValue); // Currency in string string_currency = "$357,545.45" ; console.log( "Currency value: " + string_currency); // Converting currency doubleValue = convert(string_currency); // Display currency console.log( "Converted to double: " + doubleValue); |
Currency value: $6542.45 Converted to double: 6542.45 Currency value: $357,545.45 Converted to double: 357545.45