Skip to content

Instantly share code, notes, and snippets.

@lucas-janon
Last active October 17, 2018 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucas-janon/ed7aea7e4833a4372b4f47a273d9f9c7 to your computer and use it in GitHub Desktop.
Save lucas-janon/ed7aea7e4833a4372b4f47a273d9f9c7 to your computer and use it in GitHub Desktop.
Format number as currency (USD) and format USD as ARS, avoids using toLocaleString (compatibility)
const numberAsUsd = price => price.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'); // numberAsUsd(1000) === 1,000
const usdToArs = price => price.replace(/[,.]/g, x => (x === ',' ? '.' : ',')); // usdToArs(1,000) === 1.000
const deleteZerosAfterComma = price => price.replace(',00', ''); // deleteZerosAfterComma(1.000,00) === 1.000
export const numberAsArs = price => `$ ${deleteZerosAfterComma(usdToArs(numberAsUsd(price)))}`; // numberAsArs(1000) === '$ 1.000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment