-
Notifications
You must be signed in to change notification settings - Fork 73
Option to Remove .00
#31
Comments
Great suggestion, this should work for now until I get the chance to add the option. function precision(value) {
return value % 1 === 0 ? 0 : null
}
currencyFormatter.format(price, { code: 'USD', precision: precision(price) }) |
@smirzaei The above Also, it does not seem to work for $12.20 -> $12.2(output value = $12.20). Here is the code: var currencyFormatter = require('currency-formatter');
function precision(value) {
return value % 1 === 0 ? 0 : null;
}
var value = 12.00;
var nextValue = 12.20;
var anotherValue = 12.43566;
var currency = currencyFormatter.format(value, {code: 'USD', precision: precision(value)});
var nextCurrency = currencyFormatter.format(nextValue, {code: 'USD', precision: precision(nextValue)});
var anotherCurrency = currencyFormatter.format(anotherValue, {code: 'USD', precision: precision(anotherValue)});
console.log('Currency: ' + currency);
console.log('Next currency: ' + nextCurrency);
console.log('Another currency: ' + anotherCurrency); Output: Currency: $12
Next currency: $12.20
Another currency: $12.44
|
@sanjeevkpandit I'm not exactly sure what you are asking but I'm guessing that you want to always hide the decimal points. The function above is for removing the decimal points only if it's currencyFormatter.format(value, {code: 'USD', precision: 0}); |
@smirzaei I got your point. So, basically we have to pre-define the precision value else, there are always two decimal points even if the input(not having 0 as decimal values) has 1 or more decimal points. For e.g. 12.43566 will return $12.44 not $12.43566. Am I right here? |
@sanjeevkpandit Yes, the |
@smirzaei Okay. But, the thing is that the output gives exactly 2 decimal points even if the input contains more than 2 decimal points. Is this okay or the output should contain the exact number of decimal points as of input? |
@sanjeevkpandit this is exactly the purpose of this library, to show price in a format which makes sense for the user. In your example I would say this is okay for 99% of the use cases unless you need to show very accurate pricing information and include the mills, like accounting for example. |
@smirzaei Got it. Thanks for clearing this out for me. |
It would be nice if there was a option to remove zero cents, but leave cents if there was a value.
The text was updated successfully, but these errors were encountered: