Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. {{ item.price | number }}
  2.  
  3. <input type="text" name="price" [ngModel]="price | number" (ngModelChange)="price=$event" >
  4.  
  5. import { Pipe, PipeTransform } from '@angular/core';
  6.  
  7. @Pipe({
  8. name: 'numberPipe'
  9. })
  10. export class NumberPipePipe implements PipeTransform {
  11.  
  12. transform(val) {
  13. val = this.format_number(val, '');
  14. return val;
  15. }
  16.  
  17. format_number(number, prefix) {
  18. let thousand_separator = ',',
  19. decimal_separator = '.',
  20. regex = new RegExp('[^' + decimal_separator + '\d]', 'g'),
  21. number_string = number.replace(regex, '').toString(),
  22. split = number_string.split(decimal_separator),
  23. rest = split[0].length % 3,
  24. result = split[0].substr(0, rest),
  25. thousands = split[0].substr(rest).match(/d{3}/g);
  26.  
  27. if (thousands) {
  28. let separator = rest ? thousand_separator : '';
  29. result += separator + thousands.join(thousand_separator);
  30. }
  31. result = split[1] != undefined ? result + decimal_separator + split[1] : result;
  32. return prefix == undefined ? result : (result ? prefix + result : '');
  33. };
  34. }
Add Comment
Please, Sign In to add comment