currency-formatter icon indicating copy to clipboard operation
currency-formatter copied to clipboard

Option to Remove `.00`

Open kgn opened this issue 7 years ago • 8 comments

It would be nice if there was a option to remove zero cents, but leave cents if there was a value.

30 > $30
12.43 > $12.43
12.43543 > $12.43543
12.0000 > $12

kgn avatar Feb 03 '17 05:02 kgn

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 avatar Feb 03 '17 19:02 smirzaei

@smirzaei The above precision function works for $12.00 -> $12, but does not seem to work for $12.435 -> $12.435(output result = $12.44). It always returns value with 2 decimal places for precision=null instead of returning the exact number of decimal places in input value.

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 avatar Apr 05 '17 11:04 sanjeevkpandit

@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 0. You can use precision: 0 to always hide it.

currencyFormatter.format(value, {code: 'USD', precision: 0});

smirzaei avatar Apr 05 '17 15:04 smirzaei

@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 avatar Apr 05 '17 15:04 sanjeevkpandit

@sanjeevkpandit Yes, the precision option is there for you to force the number of decimal points that you want to display. Otherwise it will read it from the currency data. (which is 2 in most cases)

smirzaei avatar Apr 05 '17 16:04 smirzaei

@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 avatar Apr 05 '17 16:04 sanjeevkpandit

@sanjeevkpandit this is exactly the purpose of this library, to show price in a format which makes sense for the user. In your example $12.43566 looks unconventional.

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 avatar Apr 05 '17 17:04 smirzaei

@smirzaei Got it. Thanks for clearing this out for me.

sanjeevkpandit avatar Apr 05 '17 17:04 sanjeevkpandit