package com.jlb.scan.util; import java.text.DecimalFormat; public class WeightFormatUtil { public static String format(float number) { String str = number + ""; int pos = str.indexOf("."); if (pos < 0) return str; else { String pre = str.substring(0, pos); String last = str.substring(pos + 1); try { int xiaoshu = Integer.parseInt(last); if (xiaoshu == 0) { return pre; } else { StringBuilder sb = new StringBuilder(last); int i = last.length(); char[] chars = last.toCharArray(); while (i-- > 0) { String t = sb.substring(i,i+1); if (t.equals("0")) { sb.delete(i,i+1); } else { break; } } return pre + "." + sb.toString(); } } catch (NumberFormatException ex) { ex.printStackTrace(); } } DecimalFormat df = new DecimalFormat("##0.000"); return df.format(number); } }