101

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

1
  • since the <input> has type="number", if i do toFixed(2) it will throw an error. :(
    – WABBIT0111
    Commented Feb 19, 2016 at 19:31

9 Answers 9

90

If you are using Angular 2 (apparently it also works for Angular 4 too), you can use the following to round to two decimal places{{ exampleNumber | number : '1.2-2' }}, as in:

<ion-input value="{{ exampleNumber | number : '1.2-2' }}"></ion-input>

BREAKDOWN

'1.2-2' means {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}:

  • A minimum of 1 digit will be shown before decimal point
  • It will show at least 2 digits after decimal point
  • But not more than 2 digits

Credit due here and here

0
89

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 | number : 2" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" step="0.01" />

Reading forward this was pointed on the next answer ng-model="myDecimal | number : 2".

8
  • Done, sorry I didn't consider that because I thought that having it in context was better, but then again, you are right. Commented Feb 29, 2016 at 14:23
  • 3
    that displays "1" instead of "1.00" Commented Sep 21, 2016 at 13:18
  • 8
    That doesn't work. It checks that the input has no more than 2 decimals, but doesn't change the displayed number to display the 2 decimals (even if those are 0)
    – David V.
    Commented Oct 26, 2016 at 12:36
  • 9
    Could you provide a jsfiddle? And as @jcc pointed out below, you can't use filters in ng-model. Doesn't work for me. Commented May 5, 2017 at 13:21
  • 2
    It's been mentioned elsewhere, but this doesn't work because an input of type number expects a Number as it's value, but the angular number filter returns a String. Suggesting to make the input type text is really just sidestepping the issue.
    – c24w
    Commented Jan 10, 2019 at 12:50
54
{{value | number : fractionSize}}

like {{12.52311 | number : 2}}

so this will print 12.52

1
  • 7
    It's for angular.js, which is an older version of Angular Commented Jan 16, 2020 at 18:23
43

Simply use the number pipe like so :

{{ numberValue | number : '.2-2'}}

The pipe above works as follows :

  • Show at-least 1 integer digit before decimal point, set by default
  • Show not less 2 integer digits after the decimal point
  • Show not more than 2 integer digits after the decimal point
0
29

Did you try using the filter

<input ng-model='val | number: 2'>

https://docs.angularjs.org/api/ng/filter/number

4
5

Another shorthand to (@maudulus's answer) to remove {maxFractionDigits} since it's optional.

You can use {{numberExample | number : '1.2'}}

3

best way to Round off number to decimal places is that

a=parseFloat(Math.round(numbertobeRound*10^decimalplaces)/10^decimalplaces);

for Example ;

numbertobeRound=58.8965896589;

if you want 58.90

decimalplaces is 2

a=parseFloat(Math.round(58.8965896589*10^2)/10^2);
1
1
(Math.round(2.782061 * 100) / 100).toFixed(2);

This will convert that into Two decimal places:

For example: 2.782061 ---> 2.78 two decimal Places

-1

Use currency filter with empty symbol ($)

{{val | currency:''}}
1
  • 3
    this actually won't work, because i am using <input type="number"> this will throw an error. and i cannot not use a type="number" in this case
    – WABBIT0111
    Commented Feb 23, 2016 at 18:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.