//整数相除 保留一位小数 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));
4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public class format { double f = 111231.5585; public void m1() { BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND
precise math function Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 2 Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ←→ Problem Description 喜爱ACM的PBY同学遇到了一道数学
java保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 保留两位小数 --------------------------------------------------------------- 方