当前位置: 首页>>代码示例>>Java>>正文


Java BigDecimal.remainder方法代码示例

本文整理汇总了Java中java.math.BigDecimal.remainder方法的典型用法代码示例。如果您正苦于以下问题:Java BigDecimal.remainder方法的具体用法?Java BigDecimal.remainder怎么用?Java BigDecimal.remainder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.math.BigDecimal的用法示例。


在下文中一共展示了BigDecimal.remainder方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: roundPriceToTickSize

import java.math.BigDecimal; //导入方法依赖的package包/类
/**
 * @param price        price to be rounded, has to be positive
 * @param roundingMode rounding mode that should be used (only {@link RoundingMode#UP} and {@link RoundingMode#DOWN}
 *                     are supported)
 * @return price rounded to tick size
 *
 * @throws IllegalArgumentException if {@code roundingMode} is neither {@link RoundingMode#UP} nor
 *                                  {@link RoundingMode#DOWN} or {@code price} is not positive
 */
static BigDecimal roundPriceToTickSize(final BigDecimal price, final RoundingMode roundingMode, final BigDecimal tickSize) {
    checkArgument(
        roundingMode == RoundingMode.UP || roundingMode == RoundingMode.DOWN,
        "Only rounding UP or DOWN supported"
    );
    checkArgument(price.compareTo(BigDecimal.ZERO) >= 0, "price=%s < 0", price);

    final BigDecimal remainder = price.remainder(tickSize);
    if (remainder.compareTo(BigDecimal.ZERO) == 0) {
        return price;
    }

    final BigDecimal result = price.subtract(remainder);
    if (roundingMode == RoundingMode.UP) {
        return result.add(tickSize);
    } else {
        return result;
    }
}
 
开发者ID:quedexnet,项目名称:java-market-maker,代码行数:29,代码来源:UniformFuturesOrderPlacingStrategy.java

示例2: bigDecimalOperation

import java.math.BigDecimal; //导入方法依赖的package包/类
private static BigDecimal bigDecimalOperation(BigDecimal op1, BigDecimal op2, Operation operation) {
    switch (operation) {
    case ADD:
        return op1.add(op2);
    case SUBTRACT:
        return op1.subtract(op2);
    case MULTIPLICATION:
        return op1.multiply(op2, MathContext.DECIMAL128);
    case DIVISION:
        return op1.divide(op2, MathContext.DECIMAL128);
    case MODULUS:
        return op1.remainder(op2, MathContext.DECIMAL128);
    default:
        throw new RuntimeException("Bug in OperatorUtils in pebble library");
    }
}
 
开发者ID:flapdoodle-oss,项目名称:de.flapdoodle.solid,代码行数:17,代码来源:OperatorUtils.java

示例3: eval

import java.math.BigDecimal; //导入方法依赖的package包/类
private BigDecimal eval(Interpreter interpreter, HashMap<String, BigDecimal> arguments, HashSet<String> reserved) throws SyntaxException, ArithmeticException, StackOverflowError {
    if (value == null) {
        BigDecimal l = left.eval(interpreter, arguments);
        BigDecimal r = right.eval(interpreter, arguments);
        switch (operator) {
            case '-': return l.subtract(r);
            case '+': return l.add(r);
            case '*': return l.multiply(r);
            case '%': return l.remainder(r);
            case '/': return l.divide(r, interpreter.getInternalRounding(), RoundingMode.HALF_UP);
            case '^': return l.pow(r.intValue());
        }
    }
    if (reserved.contains(value))
        throw new SyntaxException("Variable '" + value + "' is reserved");
    if(arguments.containsKey(value))
        return arguments.get(value);

    HashMap<String, BigDecimal> variables = interpreter.getVariables();
    if(variables.containsKey(value))
        return variables.get(value);
    if (value.contains("(")) {
        HashMap<String, Function> functions = interpreter.getFunctions();
        String name = value.split("\\(")[0];
        if(!functions.containsKey(name)) throw new SyntaxException("Function '" + name + "' not defined");
        String args = value.substring(name.length() + 1, value.length() - 1);
        return functions.get(name).eval(interpreter, arguments, args);
    }
    if(value.matches("[0-9]+(\\.[0-9]*)?"))
        return new BigDecimal(value);
    throw new SyntaxException("Variable '" + value + "' not defined");
}
 
开发者ID:Baizey,项目名称:Helpers,代码行数:33,代码来源:ParserNode.java

示例4: getValueInternal

import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();
	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
		}

		if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}

		if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
		}

		if (leftNumber instanceof Long || rightNumber instanceof Long) {
			return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
		}

		return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
	}

	return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:32,代码来源:OpModulus.java

示例5: setValue

import java.math.BigDecimal; //导入方法依赖的package包/类
/**
 * 设置表盘数值
 * @param value
 * @return 返回溢出的值
 */
public synchronized BigDecimal setValue(BigDecimal value)
{
	BigDecimal in=value.abs();
	BigDecimal br=new BigDecimal(radix);
	for(int i=rotors.size()-1;i>=0;i--)
	{
		BigDecimal v=new BigDecimal(radix).pow(i);
		BigDecimal count=in.divide(v,3);
		Rotor rotor=rotors.get(i);
		if(count.compareTo(new BigDecimal(0))==0)
		{
			rotor.setCurrenIndex(0);
			in=in.remainder(v);
		}else if(count.compareTo(new BigDecimal(0))>0 && count.compareTo(br)<0)
		{
			rotor.setCurrenIndex(count.intValue());
			in=in.subtract(v.multiply(count));
		}else if(count.compareTo(br)>=0)
		{
			rotor.setCurrenIndex(rotor.getMaxIndex());
			in=in.remainder(v);
			in=in.add(v.multiply(count.subtract(br).add(new BigDecimal(1))));
		}
	}
	return in;
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:32,代码来源:ClockDial.java

示例6: getSparseFromBigDecimal

import java.math.BigDecimal; //导入方法依赖的package包/类
public static void getSparseFromBigDecimal(BigDecimal input, DrillBuf data, int startIndex, int scale, int precision, int nDecimalDigits) {

        // Initialize the buffer
        for (int i = 0; i < nDecimalDigits; i++) {
          data.setInt(startIndex + (i * integerSize), 0);
        }

        boolean sign = false;

        if (input.signum() == -1) {
            // negative input
            sign = true;
            input = input.abs();
        }

        // Truncate the input as per the scale provided
        input = input.setScale(scale, BigDecimal.ROUND_HALF_UP);

        // Separate out the integer part
        BigDecimal integerPart = input.setScale(0, BigDecimal.ROUND_DOWN);

        int destIndex = nDecimalDigits - roundUp(scale) - 1;

        // we use base 1 billion integer digits for out integernal representation
        BigDecimal base = new BigDecimal(DIGITS_BASE);

        while (integerPart.compareTo(BigDecimal.ZERO) == 1) {
            // store the modulo as the integer value
            data.setInt(startIndex + (destIndex * integerSize), (integerPart.remainder(base)).intValue());
            destIndex--;
            // Divide by base 1 billion
            integerPart = (integerPart.divide(base)).setScale(0, BigDecimal.ROUND_DOWN);
        }

        /* Sparse representation contains padding of additional zeroes
         * so each digit contains MAX_DIGITS for ease of arithmetic
         */
        int actualDigits;
        if ((actualDigits = (scale % MAX_DIGITS)) != 0) {
            // Pad additional zeroes
            scale = scale + (MAX_DIGITS - actualDigits);
            input = input.setScale(scale, BigDecimal.ROUND_DOWN);
        }

        //separate out the fractional part
        BigDecimal fractionalPart = input.remainder(BigDecimal.ONE).movePointRight(scale);

        destIndex = nDecimalDigits - 1;

        while (scale > 0) {
            // Get next set of MAX_DIGITS (9) store it in the DrillBuf
            fractionalPart = fractionalPart.movePointLeft(MAX_DIGITS);
            BigDecimal temp = fractionalPart.remainder(BigDecimal.ONE);

            data.setInt(startIndex + (destIndex * integerSize), (temp.unscaledValue().intValue()));
            destIndex--;

            fractionalPart = fractionalPart.setScale(0, BigDecimal.ROUND_DOWN);
            scale -= MAX_DIGITS;
        }

        // Set the negative sign
        if (sign == true) {
            data.setInt(startIndex, data.getInt(startIndex) | 0x80000000);
        }

    }
 
开发者ID:skhalifa,项目名称:QDrill,代码行数:68,代码来源:DecimalUtility.java

示例7: calc

import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public Object calc(Element element) {

    Object leftValue = left.calc(element);
    Object rightValue = right.calc(element);
    if (leftValue == null || rightValue == null) {
        throw new EvaluateException("operate is null,left: " + leftValue + "  right:" + rightValue);
    }
    // 左右都不为空,开始计算
    // step one think as number
    if (leftValue instanceof Number && rightValue instanceof Number) {
        // 都是整数,则执行整数除法
        if (leftValue instanceof Integer && rightValue instanceof Integer) {
            return (Integer) leftValue % (Integer) rightValue;
        }

        // 包含小数,转double执行除法
        if (leftValue instanceof Double || rightValue instanceof Double || leftValue instanceof Float
                || rightValue instanceof Float) {
            return ((Number) leftValue).doubleValue() % ((Number) rightValue).doubleValue();
        }

        // 包含BigDecimal 转bigDecimal
        if (leftValue instanceof BigDecimal || rightValue instanceof BigDecimal) {
            if (leftValue instanceof BigDecimal && rightValue instanceof BigDecimal) {
                return ((BigDecimal) leftValue).remainder((BigDecimal) rightValue);
            }

            BigDecimal newLeft = XpathUtil.toBigDecimal((Number) leftValue);
            BigDecimal newRight = XpathUtil.toBigDecimal((Number) rightValue);
            return newLeft.remainder(newRight);
        }

        // 包含长整数,且不包含小数,全部转化为长整数计算
        if (leftValue instanceof Long || rightValue instanceof Long) {
            return ((Number) leftValue).longValue() % ((Number) rightValue).longValue();
        }

        // 兜底,用double执行计算
        return ((Number) leftValue).doubleValue() % ((Number) rightValue).doubleValue();
    }

    throw new EvaluateException(
            "remainder operate must with number parameter left:" + leftValue + " right:" + rightValue);
}
 
开发者ID:virjar,项目名称:sipsoup,代码行数:46,代码来源:RemainderUnit.java

示例8: operate

import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public Object operate(SyntaxNode left, SyntaxNode right, StringContext stringContext) {

    Object leftValue = left.calculate(stringContext);
    Object rightValue = right.calculate(stringContext);
    if (leftValue == null || rightValue == null) {
        throw new EvaluateException("operate is null,left: " + leftValue + "  right:" + rightValue);
    }
    // 左右都不为空,开始计算
    // step one think as number
    if (leftValue instanceof Number && rightValue instanceof Number) {
        // 都是整数,则执行整数除法
        if (leftValue instanceof Integer && rightValue instanceof Integer) {
            return (Integer) leftValue % (Integer) rightValue;
        }

        // 包含小数,转double执行除法
        if (leftValue instanceof Double || rightValue instanceof Double || leftValue instanceof Float
                || rightValue instanceof Float) {
            return ((Number) leftValue).doubleValue() % ((Number) rightValue).doubleValue();
        }

        // 包含BigDecimal 转bigDecimal
        if (leftValue instanceof BigDecimal || rightValue instanceof BigDecimal) {
            if (leftValue instanceof BigDecimal && rightValue instanceof BigDecimal) {
                return ((BigDecimal) leftValue).remainder((BigDecimal) rightValue);
            }

            BigDecimal newLeft = XpathUtil.toBigDecimal((Number) leftValue);
            BigDecimal newRight = XpathUtil.toBigDecimal((Number) rightValue);
            return newLeft.remainder(newRight);
        }

        // 包含长整数,且不包含小数,全部转化为长整数计算
        if (leftValue instanceof Long || rightValue instanceof Long) {
            return ((Number) leftValue).longValue() % ((Number) rightValue).longValue();
        }

        // 兜底,用double执行计算
        return ((Number) leftValue).doubleValue() % ((Number) rightValue).doubleValue();
    }

    throw new EvaluateException(
            "remainder operate must with number parameter left:" + leftValue + " right:" + rightValue);

}
 
开发者ID:virjar,项目名称:vscrawler,代码行数:47,代码来源:Remainder.java

示例9: modulus

import java.math.BigDecimal; //导入方法依赖的package包/类
public static BigDecimal modulus(BigDecimal a, BigDecimal b) { return a.remainder(b); } 
开发者ID:CenPC434,项目名称:java-tools,代码行数:2,代码来源:Core.java


注:本文中的java.math.BigDecimal.remainder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。