Decimals in angular


I have the following:

<td>{{results | number:2}}</td>    => esto sale en la vista 120.00
    <input type="number" ng-model="results" step="any"/>  
    => esto sale en la vista 120
    <input type="number" ng-model="results | number:2" step="any"/>  
    => no sale nada en la vista

What do I have to do to get 120.00 out in the input?

That is, the value with two decimals.

You can consult this example that happens the same:


asked by sirdaiz 31.03.2017 в 09:40

3 answers


You can use the following library to get the result you want



You can not use the number directive the way you want ng-model="results | number: 2"

I hope you find it useful.

answered by 31.03.2017 / 16:10

Good, I leave the link of the example updated and working. link

I have set the ng-model="$scope.myDecimal" and it works correctly.

answered by 31.03.2017 в 12:20

Well, I do not handle angularJS and I hope my answer is not completely useless, but according to the example of jsfiddle, and if you observe, the second input is of type text and has disable=true , and the second yours is type number . The type text of the second input of the example should make it easier for the pipe to reflect the two decimal places.

You could try with a type text in the input and use a regex to validate that field in case the user can / should modify it and not enter only numbers, or look for some angle directive that update the% property value of the field (a ng-value or something like that.)

<input type=number step="0.01" value="120.00"></input>

Interesting your question, I will be aware if someone manages to solve it. Greetings

answered by 31.03.2017 в 17:07