显示数字始终在< input>中保留小数点后两位. [英] Display number always with 2 decimal places in <input>

查看:379
本文介绍了显示数字始终在< input>中保留小数点后两位.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ng模型的浮点值,我想始终在<input>中以2个小数位显示:

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">

当"myNumb"为小数时,这在大多数情况下都有效.但是,如果"myNumb"的小数位数少于2个(3.2)或整数(30),则不会强制显示小数点后2位.
如何在<input>字段中强制显示小数点后2位

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

推荐答案

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

  1. 设置正则表达式以使用ng-pattern验证输入.在这里,我只接受最多2个小数位且带点分隔符的数字.

<input type="number" name="myDecimal" placeholder="Decimal" ng-model="myDecimal | number : 2" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" step="0.01" />

将其转发给下一个答案 ng-model ="myDecimal | number: 2".

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

这篇关于显示数字始终在&lt; input&gt;中保留小数点后两位.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆