Java使用正则表达式去除小数点、字符串后面多余的0
发布日期:2021-06-29 11:47:01
浏览次数:2
分类:技术文章
本文共 707 字,大约阅读时间需要 2 分钟。
package test;public class TestString { public static void main(String[] args) { Float f = 1f; System.out.println(f.toString());//1.0 System.out.println(subZeroAndDot("1")); // 转换后为1 System.out.println(subZeroAndDot("10")); // 转换后为10 System.out.println(subZeroAndDot("1.0")); // 转换后为1 System.out.println(subZeroAndDot("1.010")); // 转换后为1.01 System.out.println(subZeroAndDot("1.01")); // 转换后为1.01 } /** * 使用java正则表达式去掉多余的.与0 * @param s * @return */ public static String subZeroAndDot(String s){ if(s.indexOf(".") > 0){ s = s.replaceAll("0+?$", "");//去掉多余的0 s = s.replaceAll("[.]$", "");//如最后一位是.则去掉 } return s; }}
也可:
"110101006000".replaceAll("(0)+$", "")
转载地址:https://blog.csdn.net/zzchances/article/details/105448240 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月23日 11时23分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
excel文本函数
2019-04-29
excel时间日期函数
2019-04-29
excel查找引用函数
2019-04-29
棱镜门事件
2019-04-29
拼多多远程删除用户照片事件
2019-04-29
power BI desktop
2019-04-29
中国信息化发展八大趋势(一)
2019-04-29
中国信息化发展八大趋势(二)
2019-04-29
中国信息化发展八大趋势(三)
2019-04-29
中国信息化发展八大趋势(四)
2019-04-29
电商大战二十年
2019-04-29
神奇的麦考林
2019-04-29
centos7使用rpm安装mysql遇到的error: Failed dependencies: mysql-community-common(x86-64) 5.7.9 is needed
2019-04-29
spark报错:warn util.utils::service ‘sparkUI‘ can not bind on part 4040.Attempting port 4041.4042等错误
2019-04-29
Error getting authority: Error initializing authority: Could not connect: No such file or directory
2019-04-29
编程程软件测试思维方式:如何科学制定测试计划
2019-04-29
BLE蓝牙4.0串口调试助手
2019-04-29
树莓派WIFI设置
2019-04-29
用vcgencmd获取树莓派硬件状态数据
2019-04-29