How to convert a currency string to a double with jQuery or JavaScript?


If you are working with currency values in your web application, it is important to convert them from a string representation into an actual numerical value. In this article, we will discuss how to do this using both jQuery and JavaScript. We will look at the different methods available for converting strings to doubles.

There are two popular ways to convert currency string into float string using different JavaScript inbuilt libraries. Let’s look into the following article to understand more about how to convert a currency string to a double with jQuery or JavaScript.

This is a straightforward way in which we use the loop to match each character one at a time, and if any character proves to be of the integer or numeric type,

  • We use the substring() method to extract the substring from the original string.

  • We will use the parseFloat() method to convert the string to a float double value after removing the substring.

  • By using the charCodeAt() function to obtain the Unicode value of the requested character, we can determine whether the character is of the numeric type or not.

The substring() method − The JavaScript built-in function string.substring() is used to return the portion of a given string that runs from start index to end index. Indexing begins at zero. Following is the syntax for string.substring().

string.substring(Startindex, Endindex)

The parseFloat() method − A built-in JavaScript function called parseFloat() is used to take a text and turn it into a floating point value. The function returns NaN, or not a number, if the string is empty or if its first character is not a numeric value. Up until the point where it runs into a character that isn't a Number, it actually returns a parsed floating point number. Following is the syntax for parseFloat().

parseFloat(value)

The charCodeAt() method − The Unicode character set code unit for the character that is present in the string at the specified index is returned by the str.charCodeAt() function.Following is the syntax for str.charCode()

str.charCodeAt(index)

Example

In the following example we are running the script to convert currency string to double.

<!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){
               temp = currency.substring(i);
               break;
            }
         }
         temp = temp.replace(/, /, '');
         return parseFloat(temp);
      }
      var string_currency = "$2245.45";
      document.write("Currency value: " + string_currency +"</br>");
      var doubleValue = convert(string_currency);
      document.write("Double value: " +doubleValue + "</br>");
   </script>
</body>
</html>

When the script gets executed, it will generate an output consisting of the currency string value used in the script along with the conversion of the currency string to a double value displayed on the webpage.

The replace() Method

The replace() method of JavaScript will be used in this method to replace all characters— aside from numbers and the dot (.)—with blanks (""). The parseFloat() method can then be used to convert a string to a float or double. Compared to substring() method, this approach is more effective.

Syntax

Following is the syntax for replace()

str.replace(A, B)

The JavaScript function string.replace() can be used to replace a portion of a given string with another string or a regular expression. The starting string will not change.

Example

Considering the following example, where we are using the replace() method to convert currency string to double.

<!DOCTYPE html>
<html>
<body>
   <h1>Converting Currency String To Double</h1>
   <script>
      function convert(currency){
         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>");
   </script>
</body>
</html>

On running the above script, the web-browser displays the currency string value along with the converted value of that currency string in double on the web-browser.

Updated on: 18-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements