On Wednesday, August 31, 2016 at 1:57:21 PM UTC+2, Klaus Meier wrote:
>
>
> Now with using number type, calculation is easy, but customer wants to
> formatted numbers. What is the best way of dealing with this dilemma?
>
>
TL;DR: Use CurrencyPipe to display values. Use Pipes in general to format
your values for display.
Longer answer:
Use number value, but on displaying the value, pipe it through
CurrencyPipe. You take a number as input, and then format it on display. Or
maybe even write your own pipe and format the number your way.
A few examples.
Template:
<div>{{ 12345 }}</div>
<div>{{ 12345 | currency }}</div>
<div>{{ 1000.01 | currency }}</div>
Rendered output:
<div>12345</div>
<div>USD12,345</div>
<div>USD1,000.01</div>
Result:
12345
USD12,345
USD1,000.01.
Look at the CurrencyPipe here
<https://angular.io/docs/ts/latest/api/common/index/CurrencyPipe-class.html>,
you have some formatting options, too.
Additional ramblings:
You can do calculations in this way, provided that you wanna do finance
calculations on the frontend *on purpose** at all. I assume you're aware
that JavaScript can't do math well, depending on your use case. So you have
to at the very least convert all numbers to int before calculating things,
that's the usual approach. Basically, instead of calculating values in
dollars, you use cents.
And maybe you want to do more, there are libraries to help you with that,
but if it's just money, working with whole numbers is sometimes enough.
Example of the problem:
0.1 + 0.2 === 0.3 // => false
Zlatko
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.