1.利用正则表达
复制代码
String s = “123.045600”;
if(s.indexOf(“.”) > 0){
//正则表达
s = s.replaceAll(“0+?$”, “”);//去掉后面无用的零
s = s.replaceAll(“[.]$”, “”);//如小数点后面全是零则去掉小数点
}
复制代码
- 使用NumberFormat
import java.text.NumberFormat
NumberFormat nf = NumberFormat.getInstance();
String value = nf.format(321.32100);
输出为321.321
版权声明:本文为zsg88原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。