欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)

 更新時(shí)間:2022年07月23日 16:04:33   作者:吾愛樂享  
這篇文章主要為大家介紹了JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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)文章!

相關(guān)文章

最新評(píng)論