java怎么处理大数相加

Python018

java怎么处理大数相加,第1张

package eshop.framework.util

import java.math.BigDecimal

public class AmountUtil {

// 默认除法运算精度

private static final int DEFAULT_DIV_SCALE = 2

/**

* 提供精确的加法运算。

*

* @param v1

* @param v2

* @return 两个参数的和

*/

public static double add(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1))

BigDecimal b2 = new BigDecimal(Double.toString(v2))

return b1.add(b2).doubleValue()

}

/**

* 提供精确的加法运算

*

* @param v1

* @param v2

* @return 两个参数数学加和,以字符串格式返回

*/

public static String add(String v1, String v2) {

BigDecimal b1 = new BigDecimal(v1)

BigDecimal b2 = new BigDecimal(v2)

return b1.add(b2).toString()

}

/**

* 提供精确的减法运算。

*

* @param v1

* @param v2

* @return 两个参数的差

*/

public static double subtract(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1))

BigDecimal b2 = new BigDecimal(Double.toString(v2))

return b1.subtract(b2).doubleValue()

}

/**

* 提供精确的减法运算

*

* @param v1

* @param v2

* @return 两个参数数学差,以字符串格式返回

*/

public static String subtract(String v1, String v2) {

BigDecimal b1 = new BigDecimal(v1)

BigDecimal b2 = new BigDecimal(v2)

return b1.subtract(b2).toString()

}

/**

* 提供精确的乘法运算。

*

* @param v1

* @param v2

* @return 两个参数的积

*/

public static double multiply(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1))

BigDecimal b2 = new BigDecimal(Double.toString(v2))

return b1.multiply(b2).doubleValue()

}

/**

* 提供精确的乘法运算

*

* @param v1

* @param v2

* @return 两个参数的数学积,以字符串格式返回

*/

public static String multiply(String v1, String v2) {

BigDecimal b1 = new BigDecimal(v1)

BigDecimal b2 = new BigDecimal(v2)

return b1.multiply(b2).toString()

}

/**

* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后2位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @return 两个参数的商

*/

public static double divide(double v1, double v2) {

return divide(v1, v2, DEFAULT_DIV_SCALE)

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @param scale

*表示需要精确到小数点以后几位。

* @return 两个参数的商

*/

public static double divide(double v1, double v2, int scale) {

return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN)

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式

*

* @param v1

* @param v2

* @param scale

*表示需要精确到小数点以后几位

* @param round_mode

*表示用户指定的舍入模式

* @return 两个参数的商

*/

public static double divide(double v1, double v2, int scale, int round_mode) {

if (scale <0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero")

}

BigDecimal b1 = new BigDecimal(Double.toString(v1))

BigDecimal b2 = new BigDecimal(Double.toString(v2))

return b1.divide(b2, scale, round_mode).doubleValue()

}

/**

* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @return 两个参数的商,以字符串格式返回

*/

public static String divide(String v1, String v2) {

return divide(v1, v2, DEFAULT_DIV_SCALE)

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @param scale

*表示需要精确到小数点以后几位

* @return 两个参数的商,以字符串格式返回

*/

public static String divide(String v1, String v2, int scale) {

return divide(v1, v2, DEFAULT_DIV_SCALE, BigDecimal.ROUND_HALF_EVEN)

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式

*

* @param v1

* @param v2

* @param scale

*表示需要精确到小数点以后几位

* @param round_mode

*表示用户指定的舍入模式

* @return 两个参数的商,以字符串格式返回

*/

public static String divide(String v1, String v2, int scale, int round_mode) {

if (scale <0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero")

}

BigDecimal b1 = new BigDecimal(v1)

BigDecimal b2 = new BigDecimal(v2)

return b1.divide(b2, scale, round_mode).toString()

}

/**

* 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN

*

* @param v

*需要四舍五入的数字

* @param scale

*小数点后保留几位

* @return 四舍五入后的结果

*/

public static double round(double v, int scale) {

return round(v, scale, BigDecimal.ROUND_HALF_EVEN)

}

/**

* 提供精确的小数位四舍五入处理

*

* @param v

*需要四舍五入的数字

* @param scale

*小数点后保留几位

* @param round_mode

*指定的舍入模式

* @return 四舍五入后的结果

*/

public static double round(double v, int scale, int round_mode) {

if (scale <0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero")

}

BigDecimal b = new BigDecimal(Double.toString(v))

return b.setScale(scale, round_mode).doubleValue()

}

/**

* 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN

*

* @param v

*需要四舍五入的数字

* @param scale

*小数点后保留几位

* @return 四舍五入后的结果,以字符串格式返回

*/

public static String round(String v, int scale) {

return round(v, scale, BigDecimal.ROUND_HALF_EVEN)

}

/**

* 提供精确的小数位四舍五入处理

*

* @param v

*需要四舍五入的数字

* @param scale

*小数点后保留几位

* @param round_mode

*指定的舍入模式

* @return 四舍五入后的结果,以字符串格式返回

*/

public static String round(String v, int scale, int round_mode) {

if (scale <0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero")

}

BigDecimal b = new BigDecimal(v)

return b.setScale(scale, round_mode).toString()

}

public static String doubleToString(double num, int scale, int round_mode) {

if (scale <0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero")

}

BigDecimal b = new BigDecimal(num)

return b.setScale(scale, round_mode).toString()

}

}

package com.nileader.big.entity

public class BigInt {

private String data_str//原始数据

private int digit//数据位数

private int[] data//大数

private boolean carry = false//进位标识符

/**

* setter and getter

*/

public boolean isCarry() {

return carry

}

public void setCarry(boolean carry) {

this.carry = carry

}

public String getData_str() {

return data_str

}

public void setData_str(String data_str) {

this.data_str = data_str

}

public int[] getData() {

return data

}

public void setData(int[] data) {

this.data = data

}

public int getDigit() {

return digit

}

public void setDigit(int digit) {

this.digit = digit

}

//构造方法

public BigInt(){}

public BigInt(String str_data)

{

this.setData_str(str_data)

}

//基本操作

/**

* 形成大数 初始化

*/

public void initBInt()

{

this.setDigit( this.getData_str().length() )

this.data = new int[this.getDigit()]

//将字符组成的大数逆序放入int[] data中

for(int i = 0, j=this.getDigit() i

{

// 1104 -->data[0] = '4',data[1] = '0',data[2]=1, data[3]= '1'

this.data[i] = Integer.parseInt( this.getData_str().substring(j-1,j) )

}

}

/**

* 进行大数相加操作

*/

public void add( BigInt bint)

{

//this的位数大于bint的位数

if( this.getDigit() <bint.getDigit() )

{

int[] datatemp = this.getData()

this.setData( bint.getData())

bint.setData( datatemp)

this.setDigit(this.getData().length)

bint.setDigit(bint.getData().length)

}

//将短的那个先加完

int i =0

for(i

{

int tdata = 0

//上次运算有进位

if( this.isCarry())

{

tdata = this.getData()[i] + bint.getData()[i] +1

//取消进位标识

this.setCarry(false)

}

else tdata = this.getData()[i] + bint.getData()[i]

//本次结果无进位

if(tdata <10) this.data[i] = tdata

//本次结果有进位

else if(tdata >=10)

{

this.data[i] = tdata -10

this.setCarry(true)

}

} //短的那个加完了

//剩余数的处理

for(i

{

//有个进位的

if(this.isCarry())

{

int tdata = this.data[i]+1

if(tdata >=10) this.data[i] = tdata -10

else

{

this.data[i] = tdata

this.setCarry(false)

}

}

}

//对最高位益处检测

if(this.data[this.getDigit()-1] == 0)

{

int[] tdata = new int[this.getDigit()+1]

System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit())

tdata[this.getDigit()] = 1

this.setData(tdata)

}

}

}

其中代码段

//对最高位益处检测

if(this.data[this.getDigit()-1] == 0)

{

int[] tdata = new int[this.getDigit()+1]

System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit())

tdata[this.getDigit()] = 1

this.setData(tdata)

}