How to use str.charCodeAt() and substring() method In Javascript

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);

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


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. 

Similar Reads

Methods to convert Currency String to a Double value

Table of Content Method 1: Using str.charCodeAt() and substring() method Method 2: Using JavaScript replace and parseFloat methodsMethod 3: Using Regular Expression Directly...

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

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

Method 3: Using Regular Expression Directly

This approach directly employs a regular expression to match numeric and decimal characters in the currency string. It then extracts these characters, joins them to form a numeric string, and converts it to a double value using parseFloat()....

Contact Us