Giovanni Dias
Giovanni Dias

Reputation: 90

currency pipe decimal numbers dont work

I'm using the pipe as follows

{{plansItem.price | currency:' ':symbol:digitInfo}}

it works only wrong if I enter with the value 3900 I wanted it to be 39.00 and I wanted to know how do I get the last two digits and put in the decimal

Upvotes: 0

Views: 1252

Answers (2)

Giovanni Dias
Giovanni Dias

Reputation: 90

Custom pipe used

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

@Pipe({
  name: 'currencyCustom'
})
export class CurrencyCustomPipe implements PipeTransform {

  transform(value: number,
    currencySign: string = '€ ',
    decimalLength: number = 2,
    chunkDelimiter: string = '.',
    decimalDelimiter: string = ',',
    chunkLength: number = 3): string {

      value /= 100;

      const result = '\\d(?=(\\d{' + chunkLength + '})+' + (decimalLength > 0 ? '\\D' : '$') + ')';
      // tslint:disable-next-line:no-bitwise
      const num = value.toFixed(Math.max(0, ~~decimalLength));

      return currencySign + (
        decimalDelimiter ? num.replace('.', decimalDelimiter) : num
      )
      .replace(
        new RegExp(result, 'g'),
        '$&' + chunkDelimiter
      );
  }

}

in html like this

{{plansItem.price | currencyCustom:' ':2:'.':','}}

Upvotes: 0

cbrawl
cbrawl

Reputation: 975

The currency pipe is not meant to place a decimal point in the middle of your value. It will append or round decimals depending on specified decimal places.

For instance,

{{500.566 | currency}} => $500.57
{{3900 | currency}} => $3,900.00

If you want 3900 to be 39.00, you would need something like,

{{3900 / 100 | currency}} => $39.00

or a custom pipe.

Upvotes: 1

Related Questions