I need to be able to handle currencies accurately in my ember app. For example, if my app is tallying a list of items, and the user enters $0.10 for item 1 and $0.20 for item 2, I should be storing the tally value as $0.30, not $0.30000000000000004.
For this, I wanted to convert user inputs into cents behind the scenes but still accept and display them as floating point in the textfields.
What's the best way to intercept input values in Em.TextField?
You could use a library like sinful.js to take our the weirdness of floating point arithmetic. Or just use good ole rounding
total: function() {
var sum = 0;
this.get('content').forEach(function(item) {
sum += parseFloat(item.price);
});
return (Math.round(sum * 100) / 100).toFixed(2);
}.property('[email protected]')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With