Derenik
Derenik

Reputation: 53

Convert currency to decimal number JavaScript

How can I convert currency string like this $123,456,78.3 to number like this 12345678.3 I tried to do using this pattern

let str = "$123,456,78.3";
let newStr = str.replace(/\D/g, '');

but it replaces . too.

Upvotes: 4

Views: 3128

Answers (3)

Heysem
Heysem

Reputation: 96

var str = "$123,456,78.3";
var newStr = Number(str.replace(/[^0-9.-]+/g,""));

Upvotes: 3

Aplet123
Aplet123

Reputation: 35512

You can use a negative character group:

"$123,456,78.3".replace(/[^\d.]/g, "")

Upvotes: 2

Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Use a lowercase d, and put it into a negated character group along with the ..

let str = "$123,456,78.3";
let newStr = str.replace(/[^\d.]/g, '');

console.log(newStr)

Upvotes: 2

Related Questions