Removing a comma from a number pipe in angular2

I am new to Angular 2. I am trying to display some data using angular. this is my piece of code:

  <span>Value :</span> <span>{{myvalue| number : '1.2-2'}}</span>

Above the part, a value will be displayed, for example: "124,500.00". This is fine, but I need to remove the comma and display data only as 124500.00. It is also not a type of currency.

I tried something like this and its not working

   <span>Value :</span> <span>{{myvalue| number: '.2-3''}}</span>

How can i do this? Can I use any custom channel?

Thanks in advance

+6
source share
3 answers

Actually, there seems to be no direct parameter in DecimalPipe to change or delete decimal points. It would probably be better to write your own channel to remove decimals.

, DecimalPipe ( ), , DecimalPipe ( ). ( , ).

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

@Pipe({
  name: 'noComma'
})
export class NoCommaPipe implements PipeTransform {

  transform(val: number): string {
    if (val !== undefined && val !== null) {
      // here we just remove the commas from value
      return val.toString().replace(/,/g, "");
    } else {
      return "";
    }
  }
}

.

 <span>Value :</span> <span>{{myvalue| number : '1.2-2' | noComma}}</span>

.

+11

REPLACE, (10 000 000 = 10000 000), (10 000 000 = 10000 000). , .

@Pipe({
    name: 'noComma'
})
export class NoCommaPipe implements PipeTransform {

 transform(val: number): string {
  if (val !== undefined && val !== null) {
    // here we just remove the commas from value
    return val.toString().replace(/,/g, "");
  } else {
    return "";
  }
 }
}
+2

, 2

https://angular.io/api/common/DecimalPipe

, , .

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

// the second parameter 'fr' is optional
registerLocaleData(localeFr, 'fr');

for reference: https://angular.io/guide/i18n#i18n-pipes

0
source

Source: https://habr.com/ru/post/1691411/


All Articles