WABBIT0111 WABBIT0111 - 3 months ago 101
AngularJS Question

Angular how to display number always with 2 decimal places in <input>

I have a float value for the ng-model that I would like to always display with 2 decimal places in the

<input>
:

<input ng-model="myNumb" step ="0.01" type="number">


This works for most case when "myNumb" has decimal. But it will not force display of the 2 decimal places if "myNumb" has less than 2 decimal places (3.2), or an integer(30)

How can I force a display of 2 decimal place in the
<input>
field

Answer

AngularJS - Input number with 2 decimal places it could help... Filtering:

  1. Set the regular expression to validate the input using ng-pattern. Here I want to accept only numbers with a maximum of 2 decimal places and with a dot separator.
<input type="number" name="myDecimal" placeholder="Decimal" ng-model="myDecimal" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" step="0.01" />
Comments