input 框能限制小数点后只能输入两位么和微信支付那样,超过小数点两位输入不显示么?

微信问答 文章 2021-12-01 09:40 354 0 全屏看文

AI助手支持GPT4.0

input 框能限制小数点后只能输入两位么和微信支付那样,超过小数点两位输入不显示么?Can the input box limit the number of decimal places to two digits? Like WeChat Pay, if two digits beyond the decimal point are entered, won’t it be displayed?

input 框能限制小数点后只能输入两位么和微信支付那样,超过小数点两位输入不显示么?

求大佬指点

Ask the boss for advice

鍥炵瓟锛�

Lv: Max Human:

正则匹配 就好了。

Qiubit:

用 indexof() 判断小数点的位置,然后用 substr() 截取小数点后两位,关键是要保证只能出现一个小数点:

<input type="digit" bindinput="getInput" value="{{value}}" />


page({  data: {    value: NaN  },  getInput(e) {    if (cost.indexOf('.') != -1) {      let str_ = cost.substr(cost.indexOf('.') + 1);      if (str_.indexOf('.') != -1) {       cost = cost.substr(0, cost.indexOf('.') + 1)+str_.substr(0, str_.indexOf('.'));       str_ = str_.substr(0, str_.indexOf('.'))     }      cost = cost.substr(0, cost.indexOf('.') + 1) + str_.substr(02);    }  })
_:

value变String 分割小数点后两位


-EOF-

AI助手支持GPT4.0