How to convert a currency string to a double value with jQuery or Javascript?
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
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
function convert(currency) {
let k, temp;
for (let i = 0; i < currency.length; i++) {
k = currency.charCodeAt(i);
if (k > 47 && k < 58) {
temp = currency.substring(i);
break ;
}
}
temp = temp.replace(/, /, "" );
return parseFloat(temp);
}
let string_currency = "$450.45" ;
console.log( "Currency value: " + string_currency);
let doubleValue = convert(string_currency);
console.log( "Double value: " + doubleValue);
string_currency = "$45, 645.45" ;
console.log( "Currency value: " + string_currency);
doubleValue = convert(string_currency);
console.log( "Double value: " + doubleValue);
|
Output
Currency value: $450.45
Double value: 450.45
Currency value: $45, 645.45
Double value: 45645.45
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
function convert(currency) {
let temp = currency.replace(/[^0-9.-]+/g, "" );
return parseFloat(temp);
}
let string_currency = "$6542.45" ;
console.log( "Currency value: " + string_currency);
let doubleValue = convert(string_currency);
console.log( "Converted to double: " + doubleValue);
string_currency = "$357,545.45" ;
console.log( "Currency value: " + string_currency);
doubleValue = convert(string_currency);
console.log( "Converted to double: " + doubleValue);
|
Output
Currency value: $6542.45
Converted to double: 6542.45
Currency value: $357,545.45
Converted to double: 357545.45
Last Updated :
02 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...