假設咱們要用四捨五入法保留兩位小數java
public class Main{ public static void main(String[] args){ double abc = 4.015;// we would like to get the result 4.02 System.out.println(new java.math.BigDecimal(Double.toString(abc)).setScale(2, java.math.BigDecimal.ROUND_HALF_UP).doubleValue()); } }
Reference 1code