【Java】Math工具类

Java中的Math类对于数学计算有着多种工具方法,十分方便,接下来先简要介绍下方法种类:
在这里插入图片描述

静态常量

System.out.println("自然对数E:"+Math.E);//2.718281828459045

System.out.println("圆周率:"+Math.PI);//3.141592653589793

三角函数

// 角度 --> 弧度 toRadians()
double x = 45; // 45°  45° -->  PI / 4
System.out.println("45°转换为弧度:" + Math.toRadians(x));
System.out.println(Math.PI / 4);
 
double y = 180;
System.out.println("180°转换为弧度:" + Math.toRadians(y));
System.out.println(Math.PI);
 
// 弧度 --> 弧度 toDegrees()
double z = 0.7853981633974483; // PI / 4 -->  45°
System.out.println("0.7853981633974483转换为角度" + Math.toDegrees(z));
 
// 正弦函数sin()
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.println("45° 的正弦值: " + Math.sin(radians));
 
// 余弦函数cos()
System.out.println("45° 的余弦值: " + Math.cos(radians));
 
// 反正弦值asin()
System.out.println("45° 的反正弦值: " + Math.asin(radians));
 
// 反余弦值acos()
System.out.println("45° 的反余弦值: " + Math.acos(radians));
 
// 正切值tan()
System.out.println("45° 的正切值: " + Math.tan(radians));
 
// 反正切值atan() atan2()
double m = 45;
double n = 30;
System.out.println("45° 的反正切值1: " + Math.atan(radians)); // atan()
System.out.println("反正弦值2: " + Math.atan2(m, n)); // atan2() 坐标系表示角的反正切值

指数函数

double p = 8;
double q = 3;
 
// exp()
System.out.println("e的6次幂: " + Math.exp(p)); // e^8
 
// pow()
System.out.println("8的3次幂: " + Math.pow(p, q)); // 8^3
 
// sqrt()
System.out.println("8的平方根: " + Math.sqrt(p));
 
// cbrt()
System.out.println("8的立方根: " + Math.cbrt(p)); // 2
 
// log()
System.out.println("ln(8): " + Math.log(p)); // ln(8)
 
// log10()
System.out.println("log10(8): " + Math.log10(p)); // log10(8)

取整

double d = 100.675;
double e = 100.500;
 
// 向上取整  ceil()
System.out.println("ceil(100.675): " + Math.ceil(d));
 
// 向下取整  floor()
System.out.println("floor(100.675): " + Math.floor(d));
 
// 最近的整数  rint()
System.out.println("rint(100.675): " + Math.rint(d));
System.out.println("rint(100.500): " + Math.rint(e));
 
// 四舍五入的整数  round()
System.out.println("round(100.675): " + Math.round(d));
System.out.println("round(100.500): " + Math.round(e));

其他

// min() 最小
System.out.println("min(): " + Math.min(2, 10));
 
// max() 最大
System.out.println("max(): " + Math.max(2, 10));
 
// random() 返回一个随机数
System.out.println("random(): " + Math.random());
 
// abs() 绝对值
System.out.println("abs(): " + Math.abs(-5));
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗半里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值