您的位置:首页技术文章
文章详情页

Java 如何使用正则表达式去除前导0

浏览:29日期:2022-08-14 13:25:40
我就废话不多说了,大家还是看代码吧

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

利用“########.##########”

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Java
相关文章: