Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Option to Remove .00 #31

Open
kgn opened this issue Feb 3, 2017 · 8 comments
Open

Option to Remove .00 #31

kgn opened this issue Feb 3, 2017 · 8 comments

Comments

@kgn
Copy link

kgn commented Feb 3, 2017

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
@smirzaei
Copy link
Owner

smirzaei commented Feb 3, 2017

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) })

@sanjeevkpandit
Copy link

@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

@smirzaei
Copy link
Owner

smirzaei commented Apr 5, 2017

@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});

@sanjeevkpandit
Copy link

@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?

@smirzaei
Copy link
Owner

smirzaei commented Apr 5, 2017

@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)

@sanjeevkpandit
Copy link

@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?

@smirzaei
Copy link
Owner

smirzaei commented Apr 5, 2017

@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.

@sanjeevkpandit
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants