Java 如何使用正则表达式去除前导0
更新时间:2021年04月16日 08:37:27 作者:吹牛大王历险记
这篇文章主要介绍了Java 使用正则表达式去除前导0的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
我就废话不多说了,大家还是看代码吧
String s="0000000002121210"
s=s.replaceAll("^(0+)", "");
System.out.println(s);
补充:Java中数字处理去掉末尾的0
实例如下所示:
public static String insertComma(String s, int len) {
if (s == null || s.length() < 1) {
return "";
}
NumberFormat formater = null;
double num = Double.parseDouble(s);
if (len == 0) {
formater = new DecimalFormat("###,###");
} else {
StringBuffer buff = new StringBuffer();
buff.append("###,###.");
for (int i = 0; i < len; i++) {
buff.append("#");
}
formater = new DecimalFormat(buff.toString());
}
return formater.format(num);
}
double num = 5.5500;
DecimalFormat decimalFormat = new DecimalFormat("##########.##########");
String numConverted = decimalFormat.format(num); //5.55
利用“########.##########”
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
相关文章
SpringBoot初始教程之Servlet、Filter、Listener配置详解
本篇文章主要介绍了SpringBoot初始教程之Servlet、Filter、Listener配置详解,具有一定的参考价值,有兴趣的可以了解一下2017-09-09
mybatis模糊查询之bind标签和concat函数用法详解
大家都知道bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中,接下来通过本文给大家介绍了mybatis模糊查询——bind标签和concat函数用法,需要的朋友可以参考下2022-08-08
Java中TreeSet、HashSet、Collection重写比较器的实现
比较器是一种可以对集合或数组中的元素按照自定义的方式进行排序的对象,本文主要介绍了Java中TreeSet、HashSet、Collection重写比较器的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2023-08-08


最新评论