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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java ArrayList size 1 没有元素 list.size() = 1 但是显示 All elements are null
下一篇:前端传时间类型报400

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月23日 11时23分58秒