Hone logo
Hone
Problems

Angular Custom Currency Formatting Pipe

Angular's built-in currency pipe is excellent for standard currency formatting. However, sometimes you need more specialized formatting, such as displaying very large numbers with abbreviated units (like "1.5M" for 1,500,000) or handling specific locale-based display rules not covered by the default pipe. This challenge will guide you in creating a custom pipe for such scenarios.

Problem Description

You are tasked with creating a custom Angular pipe named AbbreviatedCurrencyPipe. This pipe should take a number (representing a monetary value) as input and transform it into a string that displays the number with an abbreviated unit (e.g., K for thousands, M for millions, B for billions) if the number is large enough. The pipe should also allow for an optional currencySymbol argument to prepend to the formatted string.

Key Requirements:

  1. Abbreviation Logic:
    • Numbers less than 1000 should be displayed as is.
    • Numbers between 1000 and 999,999 should be displayed with 'K' appended (e.g., 1234 -> "1.23K").
    • Numbers between 1,000,000 and 999,999,999 should be displayed with 'M' appended (e.g., 1,500,000 -> "1.5M").
    • Numbers greater than or equal to 1,000,000,000 should be displayed with 'B' appended (e.g., 2,500,000,000 -> "2.5B").
  2. Decimal Precision: The pipe should round the number to one decimal place when using abbreviations (K, M, B).
  3. Currency Symbol: The pipe should accept an optional string argument for the currency symbol (e.g., '$', '€'). If provided, this symbol should be prepended to the output string.
  4. Input Type: The pipe should handle number or null/undefined inputs gracefully. If the input is null or undefined, it should return an empty string.
  5. Pipe Registration: The pipe should be correctly registered in an Angular module.

Expected Behavior:

When applied in an Angular template: {{ 1234.56 | abbreviatedCurrency:'$' }} should output "$1.23K" {{ 1500000 | abbreviatedCurrency }} should output "1.5M" {{ 950 | abbreviatedCurrency }} should output "950" {{ null | abbreviatedCurrency }} should output ""

Edge Cases:

  • Zero: 0 should be displayed as 0 (or $0 if symbol is provided).
  • Negative numbers: The logic should still apply, e.g., -1500 -> "-1.5K".

Examples

Example 1:

Input Value: 1,750,000
Arguments: ['$', 'K']  // (hypothetical arguments if we supported unit override)
Output: "$1.75M"
Explanation: The input number is in the millions, so it's converted to millions (1.75M). The '$' symbol is prepended.

Example 2:

Input Value: 2500
Arguments: []
Output: "2.5K"
Explanation: The input number is in the thousands, so it's converted to thousands (2.5K). No currency symbol is provided.

Example 3:

Input Value: 500
Arguments: ['$']
Output: "$500"
Explanation: The input number is less than 1000, so it's displayed as is. The '$' symbol is prepended.

Example 4:

Input Value: 3,200,000,000
Arguments: []
Output: "3.2B"
Explanation: The input number is in the billions, so it's converted to billions (3.2B).

Constraints

  • The input number will be within the range of standard JavaScript Number types.
  • The currencySymbol argument, if provided, will be a string.
  • The pipe should be implemented using TypeScript.

Notes

  • Consider how to handle the division and rounding accurately for each magnitude.
  • Think about the order of operations: first check for null/undefined, then apply abbreviation logic, then prepend the currency symbol.
  • You will need to create a new TypeScript file for your pipe, decorate it with @Pipe, and declare it in an Angular module.
  • When testing, make sure to test with various numbers, including those close to the boundaries of each abbreviation range (e.g., 999, 1000, 999999, 1000000).
Loading editor...
typescript