Skip to content

Commit

Permalink
Make handling of units more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
csillag committed May 2, 2023
1 parent 0513587 commit 7796707
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/app/hooks/useNumberFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export const useFormatNumber =
? number.toFormat(decimalPlaces, wantedFormat)
: number.toFormat(decimalPlaces, roundingMode, wantedFormat)
if (unit) {
const formattedUnit = number
.toNumber()
.toLocaleString(undefined, { style: 'unit', unit, unitDisplay: 'long' })
.split(' ')
.at(-1)
const formattedUnit = Intl.NumberFormat(undefined, {
style: 'unit',
unit,
unitDisplay: 'long',
})
.formatToParts(number.toNumber())
.find(p => p.type === 'unit')!.value
return `${numberString} ${formattedUnit}`
} else {
return numberString
Expand Down

0 comments on commit 7796707

Please sign in to comment.