robisim74 / angular-l10n

Angular library to translate texts, dates and numbers
MIT License
380 stars 59 forks source link

Number format options #329

Closed ihor-zinchenko closed 3 years ago

ihor-zinchenko commented 3 years ago

Hi! I need to implement custom number format options, how i can do that? Can you provide any example? For example i have search with 16584 results and i need transform it to 16.584 for English and Danish and for example 16 584 for French

robisim74 commented 3 years ago

Hi @ihor-zinchenko ,

this library uses Intl API. About the thousands separator, Intl API uses the standard for each language:

Intl.NumberFormat('en').format(16584)
"16,584"
Intl.NumberFormat('da').format(16584)
"16.584"
Intl.NumberFormat('fr').format(16584)
"16 584"

and you can use options to customize the behavior: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat

But for different thousands separator there is not an option (you can only disable it): you have to build your own custom formatter.

ihor-zinchenko commented 3 years ago

@robisim74 great thanks!) and one more, tell me please how i can set language for pipe? For example


{{ value | l10nNumber:locale.language:{ digits: '1.2-2', style: 'number' } }} 
``` and i want to use english format, for example 16888 should be 16.888
robisim74 commented 3 years ago

If you want force the pipe to use only english, just put:

{{ value | l10nNumber:'en':{ digits: '1.2-2', style: 'number' } }}

but the standard english format for thousand separator is comma: 16,888, not 16.888

ihor-zinchenko commented 3 years ago

@robisim74 i tried but style: number is incorrect

{{ value | l10nNumber:'en':{ digits: '1.2-2', style: 'decimal' } }}

Decimal works but one more) it display like that: 16888 -> 16,888.00, can you tell me how i can remove .00?

robisim74 commented 3 years ago

{{ value | l10nNumber:'en':{ digits: '1.0-0', style: 'decimal' } }}

ihor-zinchenko commented 3 years ago

@robisim74 thanks!)) you save my time!)

robisim74 commented 3 years ago

Ok, note that if your value is an integer, you can just use:

{{ value | l10nNumber:'en'}}

Greetings