關於BigDecimal 的不精確計算問題
view plaincopy to clipboardprint?
public static void main(String[] args){
BigDecimal bg
BigDecimal bg
BigDecimal bg
System
}
public static void main(String[] args){
BigDecimal bg
BigDecimal bg
BigDecimal bg
System
}
輸入結果為
實際結果應為
現在作了如下函數
比如
public static int getLongLength(BigDecimal numericalValue){
String sNumericalValue = numericalValue
String[] arrayValues = {
if (!(sNumericalValue
arrayValues = sNumericalValue
} else {
arrayValues[
}
if(arrayValues[ntains(
arrayValues[
}
int intLength = arrayValues[
return intLength;
}
public static BigDecimal muliply(BigDecimal bg
BigDecimal result = bg
int intLength = getLongLength(result);
MathContext context = new MathContext(intLength+scale
result = result
return result;
}
public static int getLongLength(BigDecimal numericalValue){
String sNumericalValue = numericalValue
String[] arrayValues = {
if (!(sNumericalValue
arrayValues = sNumericalValue
} else {
arrayValues[
}
if(arrayValues[ntains(
arrayValues[
}
int intLength = arrayValues[
return intLength;
}
public static BigDecimal muliply(BigDecimal bg
BigDecimal result = bg
int intLength = getLongLength(result);
MathContext context = new MathContext(intLength+scale
result = result
return result;
}需要特別指出的是 numericalValue
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26302.html