Angular 2 CurrencyPipe space between currency and number?

后端 未结 10 1123
轻奢々
轻奢々 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:56

    You can override the pipe as follows. Make sure you include this in the module

    import {Pipe, PipeTransform} from "@angular/core";
    import {CurrencyPipe} from "@angular/common";
    
    
    @Pipe({name: 'currency'})
    export class MyCurrencyPipe extends CurrencyPipe implements PipeTransform {
      transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
        const currencyFormat = super.transform(value, currencyCode, symbolDisplay, digits);
        const firstDigit = currencyFormat.search(/\d/);
        return currencyFormat.substring(0, firstDigit) + ' ' + currencyFormat.substr(firstDigit);
      }
    }
    

提交回复
热议问题