package cn.sasa.demo3; import java.text.DecimalFormat; public class MathDemo { public static void main(String[] args) { //返回平方根 double sqrtNum = Math.sqrt(4); System.out.println(sqrtNum); //向下取整 double floorNum = Math.floor(1.8); System.out.println(floorNum); //向上取整 double ceilNum = Math.ceil(1.1); System.out.println(ceilNum); //四舍五入 double roundNum = Math.round(4.5); System.out.println(roundNum); //绝对值 double absNum = Math.abs(-7); System.out.println(absNum); //a的b次方 double powNum = Math.pow(2, 3); System.out.println(powNum); //保留2位小数 double d1 = 3.141592653; // DecimalFormat dformat = new DecimalFormat("#.##"); // double dfNum = Double.parseDouble(dformat.format(d1)); double dfNum = Double.parseDouble(String.format("%.2f", d1)); System.out.println(dfNum); } }