java常用的Math方法记录

来源:互联网 发布:淘宝羊皮女背包 编辑:程序博客网 时间:2024/06/09 21:32
 

//取 绝对值 Math.abs(-1);

//取 两者最小值 Math.min(1, 2);

//取 两者最大值 Math.max(1, 2);

//random 取得一个 在[0.0, 1.0)之间 的随机数 Math.random();

//不大于的最大整数   Math.ceil(1.5)=2.0 //不大于的最大整数   Math.floor(1.6)=1.0

//取 平方根的值 Math.sqrt(4);

//幂运算 2的3次幂 Math.pow(2, 3);

//四舍五入运算 Math.round(2.1);//2 Math.round(2.5);//3

 //小数点 保留位
      DecimalFormat de=new DecimalFormat("0.0");//取一位,如要取多位,写多几个0上去       de.format(1.234);

//补充,产生 Min和Max之间的 数字

Math.random()*(Max-Min)+Min)


String.Format

    String.format("%1$.2f", 12.12555);// 12.13  

    String.format("我叫%s,她叫%s", "小明","小方"); // 我叫小明,她叫小方  


0 0