JAVA?biginteger類(lèi)bigdecimal類(lèi)的使用示例學(xué)習(xí)
biginteger類(lèi)的使用
A:BigInteger的概述
可以讓超過(guò)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的概述
* 可以讓超過(guò)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類(lèi)的使用
A:BigDecimal的概述
- * 由于在運(yùn)算的時(shí)候,float類(lèi)型和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類(lèi)的構(gòu)造方法和成員方法使用
BigDecimal的案例代碼
package com.fenxiangbe.regex;
import java.math.BigDecimal;
public class Demo_BigDecimal {
/**
* A:BigDecimal的概述
* 由于在運(yùn)算的時(shí)候,float類(lèi)型和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類(lèi)的構(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
//通過(guò)value of方法也可以運(yùn)算
BigDecimal bd5 = BigDecimal.valueOf(2.0);
BigDecimal bd6 = BigDecimal.valueOf(1.1);
System.out.println(bd5.subtract(bd6));//推薦 ,用這兩種方法都可以
}
}以上就是JAVA biginteger類(lèi)bigdecimal類(lèi)的使用示例學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于JAVA biginteger bigdecimal類(lèi)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java BigInteger類(lèi),BigDecimal類(lèi),Date類(lèi),DateFormat類(lèi)及Calendar類(lèi)用法示例
- JAVA基本類(lèi)型包裝類(lèi) BigDecimal BigInteger 的使用
- Java你不了解的大數(shù)型BigInteger與BigDecimal類(lèi)
- Java Big Number操作BigInteger及BigDecimal類(lèi)詳解
- Java中BigInteger與BigDecimal類(lèi)用法總結(jié)
- java數(shù)學(xué)類(lèi)Math?BigInteger?BigDecimal使用介紹
- Java中的System類(lèi)、BigInteger類(lèi)和BigDecimal類(lèi)詳解
- JavaAPI中BigInteger、BigDecimal的使用方法及應(yīng)用
相關(guān)文章
java設(shè)計(jì)模式之簡(jiǎn)單工廠模式簡(jiǎn)述
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之簡(jiǎn)單工廠模式,簡(jiǎn)單工廠模式的實(shí)質(zhì)是由一個(gè)工廠類(lèi)根據(jù)傳入的參數(shù),動(dòng)態(tài)決定應(yīng)該創(chuàng)建哪一個(gè)產(chǎn)品類(lèi)的實(shí)例,感興趣的小伙伴們可以參考一下2016-08-08
解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問(wèn)題
這篇文章主要介紹了解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
基于JAVA的短信驗(yàn)證碼api調(diào)用代碼實(shí)例
這篇文章主要為大家詳細(xì)介紹了基于JAVA的短信驗(yàn)證碼api調(diào)用代碼實(shí)例,感興趣的小伙伴們可以參考一下2016-05-05
Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題
這篇文章主要介紹了Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題,關(guān)于Cors跨域的問(wèn)題,前端有代理和jsonp的常用方式解決這種非同源的訪問(wèn)拒絕策略2023-04-04
SpringSecurity OAtu2+JWT實(shí)現(xiàn)微服務(wù)版本的單點(diǎn)登錄的示例
本文主要介紹了SpringSecurity OAtu2+JWT實(shí)現(xiàn)微服務(wù)版本的單點(diǎn)登錄的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Java讀取網(wǎng)頁(yè)內(nèi)容并下載圖片的實(shí)例
這篇文章主要介紹了Java讀取網(wǎng)頁(yè)內(nèi)容并下載圖片的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
springboot實(shí)現(xiàn)簡(jiǎn)單的消息對(duì)話的示例代碼
本文主要介紹了springboot實(shí)現(xiàn)簡(jiǎn)單的消息對(duì)話的示例代碼,可以使用WebSocket技術(shù),WebSocket是一種在客戶端和服務(wù)器之間提供實(shí)時(shí)雙向通信的協(xié)議,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

