Angular 2 CurrencyPipe space between currency and number?

后端 未结 10 1082
轻奢々
轻奢々 2021-02-18 21:08

I noticed that there is a pipe called CurrencyPipe in Angular 2, which will filter some decimals from a number. This also adds the ISO currency indicator, ie \'USD\' or any othe

10条回答
  •  粉色の甜心
    2021-02-18 21:52

    Make your own custom currency pipe.

    import { Pipe, PipeTransform } from '@angular/core';
    import { CurrencyPipe } from '@angular/common';
    
    @Pipe({ name: 'myCurrency' })
    export class MyCurrencyPipe implements PipeTransform {
        transform(num: any, currencyCode: string, showSymbol: boolean, digits: string): any {
            let value = new CurrencyPipe(navigator.language).transform(num, currencyCode, showSymbol, digits);
            let firstDigit = value.match(/\d/);
            let symbol = value.slice(0, firstDigit.index);
            let amount = value.slice(firstDigit.index);   
            return symbol + " " + amount;
        }
    }
    

    and use it in HTML like

    {{amount | myCurrency: currencyCode :true:'1.0-2'}}
    

提交回复
热议问题