2

I have a use case in which there is a input text element in which if a person enters 2345 it should be displayed as 2,345 but in back end it should store the number as 2345. How could we achieve this in Angular 2?

4
  • You can use toLocaleString() - docs Commented Jan 18, 2018 at 11:25
  • just do such as the value corresponds to what's displayed (if input text is 2,345, value would be the same), but filter the , and eventuelly convert to number when you actually use it, much simpler Commented Jan 18, 2018 at 11:25
  • stackoverflow.com/a/2901298/6306281 Commented Jan 18, 2018 at 11:25
  • I also used toLocaleString() to add commas to the number but the issue was when I was trying to get the value of input element it is giving the value with commas. And a lot of code has been written so it will require a lot of changes to remove the commas from all occurrences. So I thought if there is a way we could display the number with commas only on UI without changing much. I also tried custom directives but same issue. Commented Jan 18, 2018 at 11:53

3 Answers 3

2

Using angular inbuilt filter pipe for numbers will do it https://docs.angularjs.org/api/ng/filter/number

{{1234 | number}}

Output

1,234
Sign up to request clarification or add additional context in comments.

2 Comments

OP is asking for Angular 2 so you should describe the usage of pipes as described here
I think we can't use pipes on input elements
0

You can make a directive, that use @HotListener blur and focus

@Directive({ selector: "[app-number]" })
export class NumberFormat implements OnInit {

  private el: HTMLInputElement;
  constructor(private elementRef: ElementRef) {
    this.el = this.elementRef.nativeElement;
  }

  ngOnInit() {
    this.el.value = this.transform(this.el.value);
  }

  @HostListener("focus", ["$event.target.value"])
  onFocus(value) {
    this.el.value = this.parse(value); // opossite of transform
  }

  @HostListener("blur", ["$event.target.value"])
  onBlur(value) {
    this.el.value = this.transform(value);
  }

  //value --> value formatted
  transform(value:any) 
  {
     ...return your value formated...
  }
  //value formated--> value 
  parse(valueFormated:any){
      ..return the real value....
  }

1 Comment

I also made one custom directive which was formatting the input on key down event and formatting the input as the user types but the problem began when I tried to get the value of the input after user tabs out. Fetched value contains commas which is causing other functionality to break as they are expecting only a number.
-1

Hope this would be useful:

Index.html:

<body ng-app="numberFilterExample">
  <script>
  angular.module('numberFilterExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.val = 1234.56789;
    }]);
</script>
<div ng-controller="ExampleController">
  <label>Enter number: <input ng-model='val'></label><br>
  Default formatting: <span id='number-default'>{{val | number}}</span><br>
  No fractions: <span>{{val | number:0}}</span><br>
  Negative number: <span>{{-val | number:4}}</span>
</div>
</body>
</html>

Protractor.js

 it('should format numbers', function() {
   expect(element(by.id('number-default')).getText()).toBe('1,234.568');
   expect(element(by.binding('val | number:0')).getText()).toBe('1,235');
   expect(element(by.binding('-val | number:4')).getText()).toBe('-1,234.5679');
 });

 it('should update', function() {
   element(by.model('val')).clear();
   element(by.model('val')).sendKeys('3374.333');
   expect(element(by.id('number-default')).getText()).toBe('3,374.333');
   expect(element(by.binding('val | number:0')).getText()).toBe('3,374');
   expect(element(by.binding('-val | number:4')).getText()).toBe('-3,374.3330');
});

2 Comments

Please provide some explanation with your answer - otherwise this should be a comment
sure, will take care for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.