1

I want unformat string back into the number If with USD all works from the box with accounting library, but for example for russian ruble it fails. String examples are 1,00 руб. (RUB) and $20.00 (USD) So, I could parse those string and get currency code (in case I might need to provide it to the library) with for example this code:

I tried accounting and numbro libraries

// getting currency code
let init = (data.orderAmount).indexOf('(')
let fin = (data.orderAmount).indexOf(')')
console.log(data.orderAmount).substr(init + 1, fin - init - 1)

If I for example pass 1,00 руб. (RUB) into the accounting.unformat I would get 100 instead of 1, but for usd example it would work and result would produce 20. Same result with numbro, only need to also remove part in () in that case.

What I really want is solution, where I could pass currency code and string, and get correct number from the string for the given currency.

I tried globalize library numberParser and currencyFormatter without any success

Anonymous
  • 69
  • 12
  • You need to parse with different locales since the deliters are different (commas vs period) - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat – doublesharp Jun 04 '19 at 16:41
  • I surely tried `Intl.NumberFormat` first but its for formatting, but not for reverse action. Or I need to use it in some 200 iq way? – Anonymous Jun 04 '19 at 16:43
  • in `1,00 руб. (RUB)` do you want to get 100 or just 1? – Dananjaya Ariyasena Jun 04 '19 at 16:43
  • @DananjayaAriyasena 1,00 means 1 rouble and no cents. Comma in that locale is decimal separator – Lelio Faieta Jun 04 '19 at 16:46
  • Possible duplicate of [How can I parse a string with a comma thousand separator to a number?](https://stackoverflow.com/questions/11665884/how-can-i-parse-a-string-with-a-comma-thousand-separator-to-a-number) – doublesharp Jun 04 '19 at 16:56

3 Answers3

0

let text_1 = '1,00 руб. (RUB)';
let text_2 = '$20.00 (USD)';

console.log( text_1.match(/\d+(\,|\.)/g)[0].replace(/\,|\./g, '') )
console.log( text_2.match(/\d+(\,|\.)/g)[0].replace(/\,|\./g, '') )

Can you try this code? It matches digits until . or , and replace them.

0
function reverseFormatNumber(val,locale){
        var group = new Intl.NumberFormat(locale).format(1111).replace(/1/g, '');
        var decimal = new Intl.NumberFormat(locale).format(1.1).replace(/1/g, '');
        var reversedVal = val.replace(new RegExp('\\' + group, 'g'), '');
        reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.');
        return Number.isNaN(reversedVal)?0:reversedVal;
    }

console.log(reverseFormatNumber('1,00','ru'));```

Still would need to cut part from 1,00 руб. (RUB) string, but shouldn't be a problem

Anonymous
  • 69
  • 12
0

Seeing your necessity, I could come up with something more elaborate. code to format currency based on your inputted currency

const data = [{
  'orderAmount': '1,00 руб. (RUB)'
}, { 
  'orderAmount': '$20.00 (USD)'
}]

const getCurrency = (abbr) => {
  const enumerable = {
    rub: 'ru-RU',
    usd: 'en-US'
  } 
  try {
    return enumerable[abbr.toLowerCase().substr(1,3)]
  } catch {
    throw new Error ('Invalid currency')
  }
}

const getCurrencyCode = (abbr) => abbr.toUpperCase().substr(1,3)

const sanitizeCurrency = (value) => {
  const sanitized = value.replace(',','.').match(/\d+/g).join('.')
  return Number(sanitized);
}


const currency = data.map(item => {
  const newItem = item.orderAmount.split(' ');
  const options = { 
      style: 'currency',
      currency: getCurrencyCode(newItem[newItem.length - 1]),
    };
  const numeralFormat = new Intl.NumberFormat(
    getCurrency(newItem[newItem.length - 1]),
    options
  )

  return numeralFormat.format(sanitizeCurrency(newItem[0]))
})

console.log(currency)

in case you want to play with it https://repl.it/@Evandronao/WellmadeMetallicFlashdrives