简体   繁体   中英

How to convert a currency to number

Based off a simlar question asked here - How to convert a currency string to a double with jQuery or Javascript? , I need to take a currency that can be in the format of

-143,000.00 and convert it for 2 scenarios:

  1. Where the output would be -143000.00
  2. Where the output would be -1430000

I am not good at all with regular expressions so I have tried some hack such as these 2 below:

  1. I need to remove all of the currency except for the -
const value = "-143,000.00";
Number(value.replace(/[^0-9.,]+/g, ''))

and my second use case is I need to remove all but keep the - and the .

const value = "-143,000.00";
Number(value.replace(/[^0-9.-]+/g, ''));

Thank you for assistance that you can give.

Is this what you are looking for?

 const value = "-143,000.00"; var n1 = value.replace(/([^\d.+-]+)/g,""); var n2 = Number(n1); console.log("n1: ",n1) // "-143000.00" console.log("n2: ",n2) // -143000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM