java数值操作

package com.basic;
import static java.lang.Math.*;
import java.math.BigDecimal;
public class MathT {
public void simpleValue(){
//long a=333.33L;X
long a=333L;//不加l默认为int
float b=2.33f;//必须写f、F
float totalPrice = 0l;
double c=1111.22;//默认可以写d、D
System.out.println(Integer.toBinaryString(126));//二进制
System.out.println(Integer.toHexString(126));//十六进制
System.out.println(Integer.toOctalString(126));//八进制
}
public void simpleMath(){
System.out.println(round(5.45));//5 round(double a) 返回最接近参数的 long、round(float a) 返回最接近参数的 int。
System.out.println(round(5.5));//6
System.out.println(round(5.55));//6
System.out.println(ceil(5.1));//6.0返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数
System.out.println(ceil(5.5));//6.0
System.out.println(ceil(5.55));//6.0
System.out.println(floor(5.1));//5.0
System.out.println(floor(5.5));//5.0
System.out.println(floor(5.99999));//5.0
}

static public void P_Random(){
for (int i = 0; i < 10; ++i) {
int intmethod = (int)(Math.random() * 11.0D - 1D);// 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
if (intmethod == 10)
System.out.println("ok");
System.out.print(intmethod + ",");
}
}
public static void MovDirection(){
System.out.println(1<<35);//8 对于移位运算要进行32模运算
System.out.println(1<<3);//8 移动时用0来填充右边空出的位
System.out.println(-3>>1);//-2 移动时用符号位来填充左边空出的位
System.out.println(-3>>>1);//2147483646 移动时用0来填充左边空出的位
//注:无<<<
}
//大数值
public static void bigDecimalT(){
BigDecimal op1=new BigDecimal("3.14159");
BigDecimal op2=new BigDecimal("3");
System.out.println("和="+op1.add(op2));//6.14159
System.out.println("差="+op1.subtract(op2));//0.14159
System.out.println("积="+op1.multiply(op1));
System.out.println("商="+op1.divide(op2, BigDecimal.ROUND_UP));//1.04720
System.out.println("负值="+op1.negate());//-3.14159
System.out.println("指定精度的商="+op1.divide(op2,15, BigDecimal.ROUND_UP));//1.047196666666667

}
public static void main(String[] args) {
MathT m=new MathT();
m.simpleValue();
m.simpleMath();
P_Random();
MovDirection();
bigDecimalT();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值