JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)
biginteger類的使用
A:BigInteger的概述
可以讓超過Integer范圍內(nèi)的數(shù)據(jù)進(jìn)行運(yùn)算
B:構(gòu)造方法
public BigInteger(String val)
C:成員方法
* public BigInteger add(BigInteger val)
* public BigInteger subtract(BigInteger val)
* public BigInteger multiply(BigInteger val)
* public BigInteger divide(BigInteger val)
* public BigInteger[] divideAndRemainder(BigInteger val)
BigInteger案例代碼
package com.fenxiangbe.regex;
import java.math.BigInteger;
public class Demo_BigInteger {
/**
A:BigInteger的概述
* 可以讓超過Integer范圍內(nèi)的數(shù)據(jù)進(jìn)行運(yùn)算
* B:構(gòu)造方法
* public BigInteger(String val)
* C:成員方法
* public BigInteger add(BigInteger val)
* public BigInteger subtract(BigInteger val)
* public BigInteger multiply(BigInteger val)
* public BigInteger divide(BigInteger val)
* public BigInteger[] divideAndRemainder(BigInteger val)
*/
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("100");
BigInteger bi2 = new BigInteger("2");
System.out.println(bi1.add(bi2));//bi1+bi2=102
System.out.println(bi1.subtract(bi2));//bi1-bi2=98
System.out.println(bi1.multiply(bi2));//bi1*bi2=200
System.out.println(bi1.divide(bi2));//bi1/bi2=50
BigInteger[] arr = bi1.divideAndRemainder(bi2);// bi1/bi2=50....0
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}bigdecimal類的使用
A:BigDecimal的概述
- * 由于在運(yùn)算的時(shí)候,float類型和double很容易丟失精度,演示案例。
- * 所以,為了能精確的表示、計(jì)算浮點(diǎn)數(shù),Java提供了BigDecimal
- * 不可變的、任意精度的有符號(hào)十進(jìn)制數(shù)。
B:構(gòu)造方法
* public BigDecimal(String val)
C:成員方法
* public BigDecimal add(BigDecimal augend)
* public BigDecimal subtract(BigDecimal subtrahend)
* public BigDecimal multiply(BigDecimal multiplicand)
* public BigDecimal divide(BigDecimal divisor)
D:案例演示
* BigDecimal類的構(gòu)造方法和成員方法使用
BigDecimal的案例代碼
package com.fenxiangbe.regex;
import java.math.BigDecimal;
public class Demo_BigDecimal {
/**
* A:BigDecimal的概述
* 由于在運(yùn)算的時(shí)候,float類型和double很容易丟失精度,演示案例。
* 所以,為了能精確的表示、計(jì)算浮點(diǎn)數(shù),Java提供了BigDecimal
* 不可變的、任意精度的有符號(hào)十進(jìn)制數(shù)。
* B:構(gòu)造方法
* public BigDecimal(String val)
* C:成員方法
* public BigDecimal add(BigDecimal augend)
* public BigDecimal subtract(BigDecimal subtrahend)
* public BigDecimal multiply(BigDecimal multiplicand)
* public BigDecimal divide(BigDecimal divisor)
* D:案例演示
* BigDecimal類的構(gòu)造方法和成員方法使用
*/
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal(2.0);
BigDecimal bd2 = new BigDecimal(1.1);
System.out.println(bd1.subtract(bd2));//不推薦 不夠精確,2.0-1.1=0.9,但是運(yùn)行結(jié)果卻是0.899999999999999911182158029987476766109466552734375
BigDecimal bd3 = new BigDecimal("2.0");
BigDecimal bd4 = new BigDecimal("1.1");
System.err.println(bd3.subtract(bd4));//推薦,轉(zhuǎn)換成字符串進(jìn)行運(yùn)算 就能精確算出值2.0-1.1=0.9
//通過value of方法也可以運(yùn)算
BigDecimal bd5 = BigDecimal.valueOf(2.0);
BigDecimal bd6 = BigDecimal.valueOf(1.1);
System.out.println(bd5.subtract(bd6));//推薦 ,用這兩種方法都可以
}
}以上就是JAVA biginteger類bigdecimal類的使用示例學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于JAVA biginteger bigdecimal類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例
- JAVA基本類型包裝類 BigDecimal BigInteger 的使用
- Java你不了解的大數(shù)型BigInteger與BigDecimal類
- Java Big Number操作BigInteger及BigDecimal類詳解
- Java中BigInteger與BigDecimal類用法總結(jié)
- java數(shù)學(xué)類Math?BigInteger?BigDecimal使用介紹
- Java中的System類、BigInteger類和BigDecimal類詳解
- JavaAPI中BigInteger、BigDecimal的使用方法及應(yīng)用
相關(guān)文章
Java全面細(xì)致講解Cookie與Session及kaptcha驗(yàn)證碼的使用
web開發(fā)階段我們主要是瀏覽器和服務(wù)器之間來進(jìn)行交互。瀏覽器和服務(wù)器之間的交互就像人和人之間進(jìn)行交流一樣,但是對(duì)于機(jī)器來說,在一次請(qǐng)求之間只是會(huì)攜帶著本次請(qǐng)求的數(shù)據(jù)的,但是可能多次請(qǐng)求之間是會(huì)有聯(lián)系的,所以提供了會(huì)話機(jī)制2022-06-06
Jenkins初級(jí)應(yīng)用之Invoke?Phing?targets插件配置
這篇文章主要為大家介紹了Jenkins初級(jí)應(yīng)用之Invoke?Phing?targets的插件配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪<BR>2022-04-04
分析那些不講武德的SDK(構(gòu)造使用規(guī)范)
這篇文章主要為大家介紹了盤點(diǎn)分析那些不講武德的SDK(構(gòu)造規(guī)范)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
hibernate-validator后端表單數(shù)據(jù)校驗(yàn)的使用示例詳解
這篇文章主要介紹了hibernate-validator后端表單數(shù)據(jù)校驗(yàn)的使用,hibernate-validator提供的校驗(yàn)方式為在類的屬性上加入相應(yīng)的注解來達(dá)到校驗(yàn)的目的,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Java實(shí)現(xiàn)簡單的郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單的郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
java 對(duì)文件夾目錄進(jìn)行深度遍歷實(shí)例代碼
這篇文章主要介紹了java 對(duì)文件夾目錄進(jìn)行深度遍歷實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03

