currency.js
currency.js copied to clipboard
currency resolves to a object value but not string by default
Hi there! I'm new here and has some trouble with the api.
Expect
By default, currency resolves to a string value.
2.51 + .01; // => 2.5199999999999996
currency(2.51).add(.01); // => 2.52
Actual
It returns an object but not the string i expected
2.5199999999999996
{ intValue: 252,
value: 2.52,
s:
{ symbol: '$',
separator: ',',
decimal: '.',
formatWithSymbol: false,
errorOnInvalid: false,
precision: 2,
pattern: '!#',
negativePattern: '-!#',
increment: 0.01,
groups: /(\d)(?=(\d{3})+\b)/g },
p: 100 }
It works fine when i call format function.
This probably needs better documentation.
If you're working with a currency instance behind the scenes it is inherently an object. Having it returned as an object lets you chain methods: currency(1.23).add(2).multiply(3).format(). However, since there's a toString method, anywhere it's used as a string it will resolve as the string value.
Example:
var value = currency(1.23);
console.log(value); // => logs as an object
// input will contain the string "1.23"
document.getElementById('#myInput').value = value;
As you've noticed .format() gives you a little more control over when you want to display the string value.