There is no way to pass parameter to toLocaleString and remove currency symbol. so use this function instead.
var convertedNumber = num.toLocaleString('de-DE', { minimumFractionDigits: 2 });
Videos
There is no way to pass parameter to toLocaleString and remove currency symbol. so use this function instead.
var convertedNumber = num.toLocaleString('de-DE', { minimumFractionDigits: 2 });
Here how I solved this issue. When I want to format currency without any signs, I format it with the currency code and then just remove 3-chars code from the result.
export function getCurrencyFormatWithSymbol(currencyCode) {
return {
style: 'currency',
currency: currencyCode,
currencyDisplay: 'symbol',
}
}
export function getCurrencyFormatWithIsoCode(currencyCode) {
return {
style: 'currency',
currency: currencyCode,
currencyDisplay: 'code',
}
}
export function getCurrencyFormatWithLocalName(currencyCode) {
return {
style: 'currency',
currency: currencyCode,
currencyDisplay: 'name',
}
}
export function getCurrencyFormatNumbersOnly(currencyCode) {
return {
style: 'currency',
currency: currencyCode,
currencyDisplay: 'none',
}
}
export function formatCurrency (value, format, lang) {
const stripSymbols = (format.currencyDisplay === 'none')
const localFormat = stripSymbols ? {...format, currencyDisplay: 'code'} : format
let result = Intl.NumberFormat(lang, localFormat).format(value)
if (stripSymbols) {
result = result.replace(/[a-z]{3}/i, "").trim()
}
return result
}
Usage:
const format = getCurrencyFormatNumbersOnly('JPY')
formatCurrency(12345, format, 'ja')
formatCurrency(123456, format, 'ja')
formatCurrency(1234567, format, 'ja')
formatCurrency(12345678, format, 'ja')
Edit: The only minus, in this case, is the speed. On simple tasks, it will work perfectly. But if you are going to format a lot of numbers (for example, if you are fetching financial reports with raw data from backend and then format numbers according to user settings) this function can slow down your algorithms significantly and become a bottleneck on some browsers. So, test it carefully before using in production.