本文整理了Java中java.math.BigDecimal.valueOf()
方法的一些代码示例,展示了BigDecimal.valueOf()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.valueOf()
方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:valueOf
[英]Returns a new BigDecimal instance whose value is equal to val. The new decimal is constructed as if the BigDecimal(String)constructor is called with an argument which is equal to Double.toString(val). For example, valueOf("0.1") is converted to (unscaled=1, scale=1), although the double 0.1 cannot be represented exactly as a double value. In contrast to that, a new BigDecimal(0.1) instance has the value 0.1000000000000000055511151231257827021181583404541015625 with an unscaled value 1000000000000000055511151231257827021181583404541015625and the scale 55.
[中]返回一个新的BigDecimal实例,该实例的值等于val。新的decimal被构造为使用等于Double的参数调用BigDecimal(字符串)构造函数。toString(val)。例如,valueOf(“0.1”)转换为(未标度=1,标度=1),尽管双精度0.1不能精确表示为双精度值。与此相反,新的BigDecimal(0.1)实例的值为0.10000000000000055115123125782702118158340451015625,未标度的值为10000000000000055115123125782702118158340451015625,标度为55。
代码示例来源:origin: alibaba/druid
public static BigDecimal castToDecimal(Object val) {
if (val == null) {
return null;
}
if (val instanceof BigDecimal) {
return (BigDecimal) val;
}
if (val instanceof String) {
return new BigDecimal((String) val);
}
if (val instanceof Float) {
return new BigDecimal((Float) val);
}
if (val instanceof Double) {
return new BigDecimal((Double) val);
}
return BigDecimal.valueOf(((Number) val).longValue());
}
代码示例来源:origin: stackoverflow.com
Double toBeTruncated = new Double("3.5789055");
Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
.setScale(3, RoundingMode.HALF_UP)
.doubleValue();
代码示例来源:origin: cucumber/cucumber-jvm
private BigDecimal toSeconds(Long nanoSeconds) {
return BigDecimal.valueOf(nanoSeconds).divide(NANOS_PER_SECOND);
}
代码示例来源:origin: kiegroup/optaplanner
@Test
public void toInitializedScoreHSS() {
assertEquals(scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)),
scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)).toInitializedScore());
assertEquals(scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)),
scoreDefinitionHSS.createScoreUninitialized(-7, BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)).toInitializedScore());
}
代码示例来源:origin: org.apache.commons/commons-lang3
/**
* Test for {@link NumberUtils#toScaledBigDecimal(BigDecimal, int, RoundingMode)}.
*/
@Test
public void testToScaledBigDecimalBigDecimalIRM() {
assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 1 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(123.456), 1, RoundingMode.CEILING).equals(BigDecimal.valueOf(123.5)));
assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 2 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.5159), 3, RoundingMode.FLOOR).equals(BigDecimal.valueOf(23.515)));
assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 3 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525), 2, RoundingMode.HALF_UP).equals(BigDecimal.valueOf(23.53)));
assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 4 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.521), 4, RoundingMode.HALF_EVEN)
.multiply(BigDecimal.valueOf(1000))
.toString()
.equals("23521.0000"));
assertTrue("toScaledBigDecimal(BigDecimal, int, RoudingMode) 5 failed",
NumberUtils.toScaledBigDecimal((BigDecimal) null, 2, RoundingMode.HALF_UP).equals(BigDecimal.ZERO));
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testBigDecimalType() {
final BigDecimal original = BigDecimal.valueOf( 100 );
final BigDecimal copy = BigDecimal.valueOf( 100 );
final BigDecimal different = BigDecimal.valueOf( 999 );
runBasicTests( BigDecimalType.INSTANCE, original, copy, different );
}
代码示例来源:origin: apache/hive
@Test
public void testMathExprBround() throws HiveException {
double[] vArr = { 1.5, 2.5, -1.5, -2.5, 1.49, 1.51 };
for (double v : vArr) {
double v1 = RoundUtils.bround(v, 0);
double v2 = MathExpr.bround(v);
Assert.assertEquals(v1, v2, 0.00001);
double v3 = BigDecimal.valueOf(v).setScale(0, ROUND_HALF_EVEN).doubleValue();
Assert.assertEquals(v3, v2, 0.00001);
}
}
代码示例来源:origin: apache/ignite
/**
* @throws Exception If failed.
*/
@Test
public void testDecimal() throws Exception {
BigDecimal val;
assertEquals((val = BigDecimal.ZERO), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, 0)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, 0)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, 8)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, 8)), marshalUnmarshal(val));
assertEquals((val = new BigDecimal(new BigInteger("-79228162514264337593543950336"))), marshalUnmarshal(val));
}
代码示例来源:origin: apache/ignite
/**
* @throws Exception If failed.
*/
@Test
public void testNegativeScaleRoundingModeDecimal() throws Exception {
BigDecimal val;
assertEquals((val = BigDecimal.ZERO.setScale(-1, RoundingMode.HALF_UP)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE).setScale(-3, RoundingMode.HALF_DOWN)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE).setScale(-5, RoundingMode.HALF_EVEN)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Integer.MAX_VALUE).setScale(-8, RoundingMode.UP)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Integer.MIN_VALUE).setScale(-10, RoundingMode.DOWN)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Double.MAX_VALUE).setScale(-12, RoundingMode.CEILING)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Double.MIN_VALUE).setScale(-15, RoundingMode.FLOOR)), marshalUnmarshal(val));
}
代码示例来源:origin: wildfly/wildfly
@Override
public synchronized long getAverageSessionAliveTime() {
//this method needs to be synchronised to make sure the session count and the total are in sync
if(expiredSessionCount == 0) {
return 0;
}
return new BigDecimal(totalSessionLifetime).divide(BigDecimal.valueOf(expiredSessionCount), MathContext.DECIMAL128).longValue();
}
代码示例来源:origin: openhab/openhab1-addons
private PercentType byteToPercentType(int byteValue) {
BigDecimal percentValue = new BigDecimal(byteValue).multiply(BigDecimal.valueOf(100))
.divide(BigDecimal.valueOf(255), 2, BigDecimal.ROUND_HALF_UP);
return new PercentType(percentValue);
}
代码示例来源:origin: prestodb/presto
/**
* @since 2.7.3
*/
public static BigDecimal toBigDecimal(long seconds, int nanoseconds)
{
if (nanoseconds == 0L) {
// 14-Mar-2015, tatu: Let's retain one zero to avoid interpretation
// as integral number
if (seconds == 0L) { // except for "0.0" where it can not be done without scientific notation
return BigDecimal.ZERO.setScale(1);
}
return BigDecimal.valueOf(seconds).setScale(9);
}
return new BigDecimal(toDecimal(seconds, nanoseconds));
}
代码示例来源:origin: spring-projects/spring-data-book
/**
* Returns the total for the {@link LineItem}.
*
* @return
*/
public BigDecimal getTotal() {
return price.multiply(BigDecimal.valueOf(amount));
}
}
代码示例来源:origin: kiegroup/optaplanner
@Override
public HardSoftBigDecimalScore multiply(double multiplicand) {
// Intentionally not taken "new BigDecimal(multiplicand, MathContext.UNLIMITED)"
// because together with the floor rounding it gives unwanted behaviour
BigDecimal multiplicandBigDecimal = BigDecimal.valueOf(multiplicand);
// The (unspecified) scale/precision of the multiplicand should have no impact on the returned scale/precision
return new HardSoftBigDecimalScore(
(int) Math.floor(initScore * multiplicand),
hardScore.multiply(multiplicandBigDecimal).setScale(hardScore.scale(), RoundingMode.FLOOR),
softScore.multiply(multiplicandBigDecimal).setScale(softScore.scale(), RoundingMode.FLOOR));
}
代码示例来源:origin: apache/hbase
/**
* get the value which is converted to specified unit.
*
* @param unit size unit
* @return the converted value
*/
public double get(Unit unit) {
if (value == 0) {
return value;
}
int diff = this.unit.getOrderOfSize() - unit.getOrderOfSize();
if (diff == 0) {
return value;
}
BigDecimal rval = BigDecimal.valueOf(value);
for (int i = 0; i != Math.abs(diff); ++i) {
rval = diff > 0 ? rval.multiply(SCALE_BASE) : rval.divide(SCALE_BASE);
}
return rval.doubleValue();
}
代码示例来源:origin: kiegroup/optaplanner
@Test
public void withInitScore() {
assertEquals(scoreDefinitionHSS.createScoreUninitialized(-7, BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)),
scoreDefinitionHSS.createScore(BigDecimal.valueOf(-147), BigDecimal.valueOf(-258), BigDecimal.valueOf(-369)).withInitScore(-7));
}
代码示例来源:origin: org.apache.commons/commons-lang3
/**
* Test for {@link NumberUtils#toScaledBigDecimal(BigDecimal)}.
*/
@Test
public void testToScaledBigDecimalBigDecimal() {
assertTrue("toScaledBigDecimal(BigDecimal) 1 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(123.456)).equals(BigDecimal.valueOf(123.46)));
// Test RoudingMode.HALF_EVEN default rounding.
assertTrue("toScaledBigDecimal(BigDecimal) 2 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.515)).equals(BigDecimal.valueOf(23.52)));
assertTrue("toScaledBigDecimal(BigDecimal) 3 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525)).equals(BigDecimal.valueOf(23.52)));
assertTrue("toScaledBigDecimal(BigDecimal) 4 failed",
NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525))
.multiply(BigDecimal.valueOf(100)).toString()
.equals("2352.00"));
assertTrue("toScaledBigDecimal(BigDecimal) 5 failed",
NumberUtils.toScaledBigDecimal((BigDecimal) null).equals(BigDecimal.ZERO));
}
代码示例来源:origin: apache/ignite
/**
* @throws Exception If failed.
*/
@Test
public void testNegativeScaleDecimal() throws Exception {
BigDecimal val;
assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, -1)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, -2)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MAX_VALUE, -3)), marshalUnmarshal(val));
assertEquals((val = BigDecimal.valueOf(Long.MIN_VALUE, -4)), marshalUnmarshal(val));
}
代码示例来源:origin: knowm/XChange
public BigDecimal getLimit() {
if (limit.equals("false")) {
return BigDecimal.valueOf(Double.MAX_VALUE);
} else {
return new BigDecimal(limit);
}
}
代码示例来源:origin: google/guava
/**
* Computes the mean in a way that is obvious and resilient to overflow by using BigInteger
* arithmetic.
*/
private static int computeMeanSafely(int x, int y) {
BigInteger bigX = BigInteger.valueOf(x);
BigInteger bigY = BigInteger.valueOf(y);
BigDecimal bigMean =
new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
// parseInt blows up on overflow as opposed to intValue() which does not.
return Integer.parseInt(bigMean.toString());
}
内容来源于网络,如有侵权,请联系作者删除!