//整数相除 保留一位小数 public static String division(int a ,int b){ String result = ""; float num =(float)a/b; DecimalFormat df = new DecimalFormat("0.0"); result = df.format(num); return result; }
方式1:被除数转double后,除以除数,结果是一个double类型的数,将double结果按要求保留n位小数即可. 保留n位小数的写法 int a = 10; int b = 3; double res = new BigDecimal((double) a / b).setScale(2, RoundingMode.HALF_UP).doubleValue(); 方式2: 直接使用BigDecimal进行运算 int a = 10; int b = 3; BigDecimal bigA =
前言 为了获得一堆apk的大小,并与人类友好方式显示.本来是打算用以下方法,到时不能具体到保留两位小数. org.apache.commons.io.FileUtils.byteCountToDisplaySize(f.length()); Returns a human-readable version of the file size, where the input represents a specific number of bytes. If the size is over 1GB
@Test public void txfloat() { // TODO 自动生成的方法存根 int a=9; int b=7; DecimalFormat df=new DecimalFormat("0.00"); System.out.println(df.format((float)a/b)); System.out.println(df.format(a/(float)b)); System.out.println(df.format((float)a/(float)b));