在<input>中显示数字总是有2个小数位 [英] Display number always with 2 decimal places in <input>

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

问题描述

我有一个 ng-model 的浮点值,我希望在 中始终显示 2 个小数位:

当myNumb"有十进制时,这适用于大多数情况.但如果myNumb"的小数位小于 2 位 (3.2) 或整数 (30),则不会强制显示 2 位小数
如何强制在 字段中显示 2 个小数位

解决方案

AngularJS - 输入带有 2 个小数位的数字 它可以帮助...过滤:

<块引用>

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

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

解决方案

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

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

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