How to 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.

Method 1: This is a simple approach in which we will match character 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 substring() method to take out substring from original string. After taking out substring we will use parseFloat() method to convert that string to float ot 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.

    Syntax:

  • string.substring(Startindex, Endindex)

    The string.substring() is an inbuilt function in JavaScript which is used to return the part of the given string from start index to end index. Indexing start from zero (0).

  • parseFloat(value)

    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 which is not a Number.

  • str.charCodeAt(index)

    str.charCodeAt() function returns a Unicode character set code unit of the character present at the index in the string specified as the argument.

  • Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
        // JavaScript script to convert
        // string currency to double
          
        // Function to convert
        function convert(currency){
        var k, temp;
        // Loop to make substring
        for(var 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
        var string_currency = "$450.45";
        document.write("Currency value: " + string_currency +"</br>");
        // Converting currency
        var doubleValue = convert(string_currency);
          
        // Display currency 
        document.write("Double value: " +doubleValue + "</br>");
          
          
        // Currency in string
        string_currency = "$45, 645.45";
        document.write("Currency value: " + string_currency +"</br>");
        // Converting currency
        doubleValue = convert(string_currency);
          
        // Display currency 
        document.write("Double value: " + doubleValue + "</br>");
          
    </script>                                                     

    chevron_right

    
    

    Output:

    Currency value: $450.45
    Double value: 450.45
    Currency value: $45, 645.45
    Double value: 45645.45
    

    Method 2: 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 string to float or double. This method is more efficient than method 1.

      syntax:

    • str.replace(A, B)

      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.

    • parseFloat(value)

      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 which is not a Number.

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

              
    <script>
        // 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 
        var temp = currency.replace(/[^0-9.-]+/g,"");
          
        // Converting string to float
        // or double and return
        return parseFloat(temp);
          
        }
          
        // Driver code
          
        // Currency in string
        var string_currency = "$6542.45";
        document.write("Currency value: " + string_currency +"</br>");
        // Converting currency
        var doubleValue = convert(string_currency);
        // Display currency 
        document.write("Converted to double: " + doubleValue +"</br>");
          
          
        // Currency in string
        string_currency = "$357,545.45";
        document.write("Currency value: " + string_currency +"</br>");
        // Converting currency
        doubleValue = convert(string_currency);
        // Display currency 
        document.write("Converted to double: " + doubleValue +"</br>");
          
    </script>                                      

    chevron_right

    
    

    Output:

    Currency value: $6542.45
    Converted to double: 6542.45
    Currency value: $357,545.45
    Converted to double: 357545.45
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.