栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android-如何只允许一定数量的小数位

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android-如何只允许一定数量的小数位

我会使用正则表达式的功能在编辑文本本身中进行过滤。首先是正则表达式:

^-?(d{0,5}|d{0,5}.d{0,3})$

也许有多种方法可以改善这种表达方式,但这确实可以解决问题。

现在,只需在edittext中设置输入过滤器,如下所示:

final String regex = "^-?(d{0,5}|d{0,5}.d{0,3})$";((EditText)rootView.findViewById(R.id.editText1)).setFilters(new InputFilter[] {    new InputFilter() {        @Override        public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) { if (end > start) {     // adding: filter        // build the resulting text     String destinationString = destination.toString();     String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);     // return null to accept the input or empty to reject it     return resultingTxt.matches(regex) ? null : ""; } // removing: always accept return null;        }    }});

顺便说一句,我刚刚测试了这段代码,它的作用是:

  1. 用户最多可以输入8位数字。
  2. 用户输入“。”后,允许的最大十进制数字为8。

我是否正确理解您描述的问题?

-编辑

好吧,我快到了。据我了解,decimal(8,3)表示最多8位数字,包括小数点左边或右边的数字,范围从

-99999.999
99999.999
。我至少从这句话中了解到这一点
StandardSQL requires that DECIMAL(5,2) be able to store any value with five digits andtwo decimals, so values that can be stored in the salary column range from-999.99 to 999.99
。即使来自MySQL文档,MSSQL文档似乎也可以做到这一点。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454711.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号