Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format float/double with Angular

Tags:

angularjs

This may be a dumb question but I'm very new at Angular. How can I format a float/double to only show two decimals after the separator.

 <input type="text" name="whatever" value="{{model.Rate}}" />

I saw some question in SO but I believe that such a basic operation can be done in a simpler way.

like image 514
Pablo Honey Avatar asked May 07 '26 00:05

Pablo Honey


2 Answers

Try something like this

<input type="text" name="whatever" value="{{model.Rate.toFixed(2)}} />

assuming Rate is of type number

toFixed() returns a string, and always pads with zeros, so 0.toFixed(2) === "0.00"

like image 115
dark_ruby Avatar answered May 09 '26 17:05

dark_ruby


Angulars has some built-in filter to do that.

Angular doc

In your case this should be something like this :

value="{{ number_expression | number : 2}}"