I am a beginner in Angular 2.I'm trying to display some data using angular. this is my code part:
<span>Value :</span> <span>{{myvalue| number : '1.2-2'}}</span>
Above part will display Value as for eg: "124,500.00". Its ok but i need to remove comma and display data as 124500.00 only. Also this is not a currency type.
I tried some thing like this and its not working
<span>Value :</span> <span>{{myvalue| number: '.2-3''}}</span>
How can i do that?Can i use any custom pipe?
Thanks in advance
You can either write your own pipe that fully replaces the current use of the DecimalPipe (single pipe for everything) or you write a pipe that removes the commas after the DecimalPipe was used (chained pipes).
By default Angular decimal pipe rounds off the number to the nearest value using Arithmetic rounding method. This is different from JavaScript's Math. round() function.
Angular decimal pipe accepts two parameters, the decimal digit info and locale: Open the src/app/app.component.ts file and add a variable of type number as follows: export class App implements OnInit { aNumber: number = 10.123456789; constructor() { } ngOnInit() { } }
In fact it looks like there's no direct parameter to the DecimalPipe to change or remove the decimal points. It would probably be the best to write your own pipe to remove the decimal points.
You can either write your own pipe that fully replaces the current use of the DecimalPipe (single pipe for everything) or you write a pipe that removes the commas after the DecimalPipe was used (chained pipes). The last option could look something like this (I got the code from this answer, so greetings to Adrien).
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 "";
}
}
}
You can chain the pipes afterwards like this.
<span>Value :</span> <span>{{myvalue| number : '1.2-2' | noComma}}</span>
Just remember to declare your pipe in your module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With