David Casanellas
David Casanellas

Reputation: 855

Angular 5 - currencyPipe

I have a problem with the already built in CurrencyPipe from Angular.

I need to display a currency sign using the CurrencyPipe but I can't use it unless I provide an input number.

Because the CurrencyPipe uses the current locale to get the currency sign, I was thinking that the input number could be optional.

Current Behaviour:

{{ 1 | currency:'USD' }} --> $1

Needed Behaviour:

{{ null | currency:'USD' }} --> $

Does any of you know if this is possible with the default Pipe? Thanks!!

Upvotes: 30

Views: 93163

Answers (8)

Ariel Perez
Ariel Perez

Reputation: 1

I don't know your template, but if all the issue is the symbol, you can use the Material class, matPrefix before the input and put whatever symbol you need.

Ex.

<span matPrefix>$&nbsp;</span>
<input matInput 
       type="number"
       min="0"
       step="0.01"
       [value]="price | currency:'':'':'1.2-2'">

Upvotes: 0

Santosh Kumar Tanna
Santosh Kumar Tanna

Reputation: 71

Use the Angular CurrencyPipe.

{{ money | currency:'USD':'symbol' }}

Upvotes: 7

shrvn
shrvn

Reputation: 93

try this

${{amount | number: '2.2'}}

if amount=5 then output-->$5.00

if amount=null then output-->$

Upvotes: 1

lahiru dilshan
lahiru dilshan

Reputation: 928

You can replace DEFAULT_CURRENCY_CODE with your currency symbol and add a space.

app.module.ts > providers

providers: [ { provide: DEFAULT_CURRENCY_CODE, useValue: 'LKR ' } ],

Upvotes: 4

Rafael Pizao
Rafael Pizao

Reputation: 841

I solved with custom pipe (with symbol as parameter) because I need control when and how symbols shown.

import { Pipe, PipeTransform, Injectable } from '@angular/core';

@Injectable()
@Pipe({ name: '$' })
export class MoneyPipe implements PipeTransform {
  transform(value: number, symbol: string = null, decimalDelimiter: string = ",", thousandsDelimiter: string | null = "." ): any {
    if(!value) return symbol ? symbol + " -" : "";

    let roundValue = value.toFixed(Math.max(0, ~~2));
    let valueToReturn = roundValue.replace('.', decimalDelimiter);

    if(thousandsDelimiter) valueToReturn = this.setThousandsSign(valueToReturn, thousandsDelimiter);
    return symbol ? symbol + " " + valueToReturn : valueToReturn;
  }

  private setThousandsSign(value: string, delimiter: string): string {
    return value.split("").reverse().map((str, index) => {
      if(index <= 3) return str;

      return (index%3)===0 ? str+delimiter : str;
    }).reverse().join("");
  }
}

That was in HTML.

{{order.price | $}}

Using custom symbol.

{{order.price | $:'€'}}

Upvotes: 1

wessam yaacob
wessam yaacob

Reputation: 937

Update Angular 8

import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';

@Pipe({ name: 'CustomeCurrency' })
export class CustomCurrencyPipe implements PipeTransform {

    constructor(private currencyPipe: CurrencyPipe) { }
    transform(value: any, currencyCode?: string, display?: string | boolean, digitsInfo?: string, locale?: string): string {
        if (value != null)
            return this.currencyPipe.transform(value, currencyCode, display, digitsInfo, locale);
        return this.currencyPipe.transform(0, currencyCode, display, locale).split('0.00')[0];
    }
}

Try this simple custom currency pipe

{{ null | CustomeCurrency }}</p>

import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Pipe({ name: 'CustomeCurrency' })
export class CustomCurrencyPipe implements PipeTransform {

constructor(private currencyPipe: CurrencyPipe) { }

transform(value: any, currency: string, symbol: boolean = false): string {
     if (value != null)
        return this.currencyPipe.transform(value, currency, symbol);
    return this.currencyPipe.transform(0, currency, symbol).split('0.00')[0];
 }
}

Upvotes: 19

Mark Lanham
Mark Lanham

Reputation: 615

If you're getting the error message The currency pipe has been changed in Angular v5. The symbolDisplay option (third parameter) is now a string instead of a boolean. The accepted values are "code", "symbol" or "symbol-narrow". then refer to the Angular documentation for help, use the code samples on this page to fix the issue: https://angular.io/api/common/CurrencyPipe

Upvotes: 7

dev-nish
dev-nish

Reputation: 871

The currency pipe has been changed in Angular v5. The symbolDisplay option (third parameter) is now a string instead of a boolean. The accepted values are "code", "symbol" or "symbol-narrow".

Upvotes: 6

Related Questions