Last active
October 17, 2018 20:10
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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