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

时间:2017-12-18 07:08:26

标签: angularjs

I have a float value for the ng-model that I would like to always display with 4 decimal places in the <input>:

<input type="number" name="myDecimal"  ng-model="myDecimal | number : 4"  />

3 个答案:

答案 0 :(得分:0)

You can use value attribute of input type.

<input type="number" name="myDecimal"  ng-model="myDecimal" value="myDecimal | number : 4" />

答案 1 :(得分:0)

You can use public function ajaxDeleteItemAction(Request $request) { if($request->isXmlHttpRequest()){ $id = $request->get('entityId'); $em = $this->getDoctrine()->getManager(); $evenement = $em->getRepository("TunisiaMallBundle:Evenement")- >find($id); $em->remove($evenement); $em->flush(); return new JsonResponse('good'); } } to handle decimal points. You can use fraction to determine the number of decimal places. By default it is 2.

For more details ng-currency

ng-currency
var app = angular.module('plunker', ['ng-currency']);

app.controller('ApplicationController', function($scope) {
  $scope.amount = 0.431264;
});

答案 2 :(得分:0)

由于 nonAssign 错误,您的代码无效。您可以详细了解herehere

您可以使用花括号表示法截断小数。 {{ myDecimal | number: 4 }}

请注意,数字过滤器并没有真正截断小数位,而是将数字四舍五入到那么多小数位。

简而言之,根据您共享的代码,您无法在type=number中使用ng-model和过滤器。

如果您必须使用数字类型,那么我建议在将值放入输入框之前将其四舍五入。