-
-
Notifications
You must be signed in to change notification settings - Fork 356
part
option for n
fails with an error
#1165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you for your reporting! We cannot use the
if you would like to use I would like to know the use case why you would like to use |
Hi, @kazupon ! Thanks for replying! My use case is the following: We are working with high-precision numbers in my project. Precision sometimes goes up to 18 fractional digits. So I can have a We have decided that BE will provide to the FE such values as strings. And FE would have to display them to users localised. This is where i18n comes into play. Obviously I can not pass strings to My idea was to do something along the lines of const customFormatter = (value: string) => {
const asNumber = Number(value) // so precision is lost, but it's OK
const asParts = n(asNumber, { key: 'myNumberFormat', part: true })
const jsonDecimalSeparator = '.' // decimal separator used by my BE for numbers-as-string
const separatorIndex = value.indexOf(jsonDecimalSeparator) // index of a separator in the original string
const preciseInteger: string = value.slice(0, separatorIndex) // precise integer part as string
const preciseFraction: string = value.slice(separatorIndex + jsonDecimalSeparator.length, value.length) // precise fraction part as string
// here is the "clever" part - replacing parts of formatted number with their respective precise values
const asPrecisePartsFlattened = asParts.map(part => {
if (part.type === 'integer') return preciseInteger
if (part.type === 'fractions') return preciseFraction
return part.value
})
return asPrecisePartsFlattened.join('') // return a formatted string
} This can obviously be extended to add reactivity, support for whole-integer numbers, different formats, etc. Actually, while writing this, it got me thinking that folks working with (not-yet) supported currencies - e.g. cryptos of all sorts - could also benefit from this. const customCurrencies = ['Crypto1, Crypto2']
const customFormat = (value: number, currency: string) => {
if (!customCurrencies.includes(currency)) return n(value, { key: 'currencyFormat', currency })
const asParts = n(value, { key: 'currencyFormat', currency: 'USD' }) // using USD to not cause Intl error
return asParts
.map(part => {
if (part.type !== 'currency') return part
return {
...part
value: currency // replacing USD to our custom currency
}
})
.map(part => part.value) // flatten - collect parts values together
.join('') // return a formatted string
} |
Thanks for your detail use-case! |
I have implemented the support for part option in the $n() function. If these changes look good, I will proceed with updating the documentation. |
Reporting a bug?
Using
part
option for number formatter throws an error. JSfiddle below.Example usage:
Expected behavior
Reproduction
https://jsfiddle.net/qagtkzxj/39/
System Info
Screenshot
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: