question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Currency Pipe: improved digit info options

See original GitHub issue

Which @angular/* package(s) are relevant/related to the feature request?

common

Description

A common way to display currencies (at least in US) is to omit the cents if the price is a round number, or to show two digits if the price is not a round number. For example, for 3 dollars, $3, and for 3.1 dollar, $3.10.

There is no way to accomplish this with the current currency pipe, which only takes a range for the number of digits after the decimal. The only options either make the first price $3.00 and the second $3.10, or the first price $3 and the second $3.1.

Proposed solution

Extend the digitsInfo format. Currently, the format is {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}. As an alternative, we could offer {minIntegerDigits}.{allowedFractionDigits1},{allowedFractionDigits2}[,...]. So the digitsInfo for the format above would be 1.0,2.

Alternatives considered

Alternately, one could use an *ngIf to change out the element with the currency, and use two elements, each with a currency pipe to accomplish the above. Obviously, that is kind of messy.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kbrillacommented, Sep 27, 2022

I think one soultion would be to create new Pipes based on Intl API

const format = new Intl.NumberFormat("en-US", {style: 'currency', minimumFractionDigits: 2, trailingZeroDisplay: 'stripIfInteger', currency: 'USD'}); 

format.format(3)    
//  $3
format.format(3.1)
//  $3.10

If not maybe reusing the term trailingZeroDisplay option would be good for the current API with two values as in Intl API - NumberFormat NumberFormat

trailingZeroDisplay Experimental
A string expressing the strategy for displaying trailing zeros on whole numbers. The default is "auto".

"auto": keep trailing zeros according to minimumFractionDigits and minimumSignificantDigits.
"stripIfInteger": remove the fraction digits if they are all zero. This is the same as auto if any of the fraction digits is non-zero.

as for Intl API there are already polyfills for it here polyfill

0reactions
angular-robot[bot]commented, Nov 18, 2022

Thank you for submitting your feature request! Looks like during the polling process it didn’t collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular’s scope, we’d encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CurrencyPipe - Angular
Transforms a number to a currency string, formatted according to locale rules that determine group sizing and separator, decimal-point character, ...
Read more >
pipe - Angular 5 - currencyPipe - Stack Overflow
I need to display a currency sign using the CurrencyPipe but I can't use it unless I provide an input number. Because the...
Read more >
Formatting numbers, strings and currency values in ag-Grid
In this post, I'll illustrate how to format numbers, strings and currencies in ag-Grid. These are illustrated with examples you can easily ...
Read more >
Number.prototype.toLocaleString() - JavaScript | MDN
The locales and options parameters customize the behavior of the function and ... const number = 123456.789; // request a currency format ...
Read more >
2022 Publication 225 - IRS
search feature or viewing the categories listed. ... your tax return require additional information ... the pipe was laid.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found