我想将Java BigDecimal
舍入到一定数量的有效数字(非小数位),例如到4位数:
12.3456 => 12.35 123.456 => 123.5 123456 => 123500
基本的问题是如何找到BigDecimal
的数量级,所以我可以决定小数点后使用多少个地方。
我能想到的只是一个可怕的循环,除以10直到结果<1,我希望有更好的方法。
顺便说一句,这个数字可能非常大(或非常小)所以我无法将其转换为双倍以使用登录它。
最简单的解决方案是:
int newScale = 4-bd.precision()+bd.scale();
BigDecimal bd2 = bd1.setScale(newScale, RoundingMode.HALF_UP);
没有字符串转换是必要的,它完全基于BigDecimal
算术,因此尽可能高效,你可以选择RoundingMode
,它很小。如果输出应该是String
,只需附加.toPlainString()
。
为什么不使用round(MathContext)
?
BigDecimal value = BigDecimal.valueOf(123456);
BigDecimal wantedValue = value.round(new MathContext(4, RoundingMode.HALF_UP));
您可以使用以下行:
int digitsRemain = 4;
BigDecimal bd = new BigDecimal("12.3456");
int power = bd.precision() - digitsRemain;
BigDecimal unit = bd.ulp().scaleByPowerOfTen(power);
BigDecimal result = bd.divideToIntegralValue(unit).multiply(unit);
注意:此解决方案始终向下舍入到最后一位数。
有人可能会想出一个更好的解决方案,但首先想到的是将它放入StringBuilder中,检查它是否包含'。'并返回一个适当长度的子串。例如。:
int n = 5;
StringBuilder sb = new StringBuilder();
sb.append("" + number);
if (sb.indexOf(".") > 0)
{
n++;
}
BigDecimal result = new BigDecimal(sb.substring(0, n));
对我来说,这似乎很简单:给定N = 5,D = 123.456789
订单可以使用Math.floor(Math.log(D))计算。
希望这可以帮助。
由于BigDecimal基本上是一个字符串,也许这个:
import java.math.BigDecimal;
public class Silly {
public static void main( String[] args ) {
BigDecimal value = new BigDecimal("1.23238756843723E+5");
String valueString = value.toPlainString();
int decimalIndex = valueString.indexOf( '.' );
System.out.println( value + " has " +
(decimalIndex < 0 ? valueString.length() : decimalIndex) +
" digits to the left of the decimal" );
}
}
产生这个:
123238.756843723 has 6 digits to the left of the decimal
A.H.'s answer在技术上是正确的,但这是一个更通用(也更容易理解)的解决方案:
import static org.bitbucket.cowwoc.requirements.core.Requirements.assertThat;
/**
* @param value a BigDecimal
* @param desiredPrecision the desired precision of {@code value}
* @param roundingMode the rounding mode to use
* @return a BigDecimal with the desired precision
* @throws NullPointerException if any of the arguments are null
*/
public BigDecimal setPrecision(BigDecimal value, int desiredPrecision, RoundingMode roundingMode)
{
assertThat("value", value).isNotNull();
assertThat("roundingMode", roundingMode).isNotNull();
int decreaseScaleBy = value.precision() - desiredPrecision;
return value.setScale(value.scale() - decreaseScaleBy, roundingMode);
}