Add support for decimal places in currency filter #2676
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I know @yyx990803 has little intention to extend these core functionality, but IMHO you either support something fully or you don't at all. So…
Currently the "currency" filter only accepts one argument: the currency symbol. The number of decimal places is fixed as 2. This can cause troubles with several currencies out there whose number of decimal places can be 0 (e.g. Japanese Yen or Vietnamese Dong) or 3 (e.g. Jordanian Dinar).
This commit modifies the filter to accept an extra optional argument: decimal places, which defaults to 2. With this, one can write
{{ 1234 | currency '¥' 0 }}
to properly display a Yen value. Backward compatibility is maintained.(Just realized there's a StackOverflow question about this very issue, where the OP was recommended to create a new filter, which is anti-DRY IMO).