首页 > 其他分享 >BigInteger与BigDecimal类

BigInteger与BigDecimal类

时间:2023-05-25 22:34:24浏览次数:34  
标签:BigInteger BigDecimal val double 其值 public

Math类

java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回 值类型一般为double型。

abs 绝对值
acos,asin,atan,cos,sin,tan 三角函数
sqrt 平方根
pow(double a,doble b) a的b次幂
log 自然对数
exp e为底指数
max(double a,double b)
min(double a,double b)
random() 返回0.0到1.0的随机数
long round(double a) double型数据a转换为long型(四舍五入)
toDegrees(double angrad) 弧度—>角度
toRadians(double angdeg) 角度—>弧度

BigInteger与BigDecimal

Integer类作为int的包装类,能存储的最大整型值为2^31-1,Long类也是有限的, 最大为2^63-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。

java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供 所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。

构造器

BigInteger(String val):根据字符串构建BigInteger对象

常用方法

public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。
BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger
BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger
BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger
BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

BigDecimal类

一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中, 要求数字精度比较高,故用到java.math.BigDecimal类。BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

构造器

public BigDecimal(double val) 

public BigDecimal(String val)

常用方法

public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

例子

image-20230525214846474

标签:BigInteger,BigDecimal,val,double,其值,public
From: https://blog.51cto.com/u_15977171/6351553

相关文章

  • Java数字转中文数字——支持:Integer、BigDecimal
    1、效果publicstaticvoidmain(String[]args){System.out.println(int2chineseNum(3456));System.out.println(int2chineseNum(-3456));System.out.println(bigDec2ChinaNum(newBigDecimal("12367.4852")));System.out.......
  • Bigdecimal使用
    1.Bigdecimal返回数据小数后0自动被删除的问题importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackson.databind.JsonSerializer;importcom.fasterxml.jackson.databind.SerializerProvider;importjava.io.IOException;importjava.math.BigDecim......
  • Java中进行高精准度坐标数据计算使用BigDecimal(计算距离、开平方)
    场景Java中使用java.awt.geom.Point2D进行坐标相关的计算(距离、平方等):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126072919Java中使用JTS对空间几何计算(读取WKT、距离、点在面内、长度、面积、相交等):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article......
  • BigDecimal的setScale常用方法(ROUND_UP、ROUND_DOWN、ROUND_HALF_UP、ROUND_HALF_DOW
    BigDecimal的setScale四大常用方法总结//设置小数点后第三位数字一大一小观察效果BigDecimalnum=newBigDecimal("3.3235667");BigDecimalnumOne=newBigDecimal("3.3275667");1、ROUND_UP:进位制:不管保留数字后面是大是小(0除外)都会进1//ROUND_UP--进位制:不管保留数......
  • 当 SQL Server(mssql-jdbc) 遇上 BigDecimal → 精度丢失,真坑!
    开心一刻中午和哥们一起喝茶哥们说道:晚上喝酒去啊我:不去,我女朋友过生日哥们瞪大眼睛看着我:你有病吧,充气的过什么生日我生气到:有特么生产日期的好吧需求背景系统对接了外部系统,调用外部系统的接口需要付费,一个接口一次调用付费0.03元同一个......
  • Java基础知识点API之BigInteger的存储上限
    一:数组的长度相关内容1.数组的最大长度是int的最大值:2147483647.2.数组中每一位能表示的数字的范围:  -2147483648~21474836473.数组中最多能存储的元素个数:21亿多。4.数组中每一位能表示的数字:42亿多二:BigInteger能表示的最大数字通过了解上述的数组长度的内容,能更好的理解BigI......
  • Java编码规范-字符串与Integer的比较,BigDecimal非空参数
    Java编码规范-字符串与Integer的比较,BigDecimal非空参数packagecom.example.core.mydemo;importjava.math.BigDecimal;publicclassIntTest{publicstaticvoidmain(String[]args){Integertype=2;//if("2".equals(type)){if(typ......
  • BigDecimal 的floatValue 方法 返回数值 精度丢失
    packagetest;importjava.math.BigDecimal;importjava.text.DecimalFormat;publicclassA{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubBigDecimala=newBigDecimal("641825.59");System.out.p......
  • java8 BigDecimal 除法保留三位小数 (舍去小数三位后的数,四舍五入)
    BigDecimala=newBigDecimal("9");BigDecimalb=newBigDecimal("15");//舍去小数三位后的数BigDecimalc=b.divide(a,3,RoundingMode.HALF_UP);System.out.println(c);//四舍五入BigDecimal......
  • Java_Double&BigDecimal
    importjava.math.BigDecimal;importjava.math.MathContext;publicclassBigDecimalTest{/***@paramargs*@referencearchie2010*@function实现将double类型的值转换为BigDecimal类型的值的不同途径以及各途径间的区别*......