當前位置: 首頁>>代碼示例>>Java>>正文


Java BigDecimal.scale方法代碼示例

本文整理匯總了Java中java.math.BigDecimal.scale方法的典型用法代碼示例。如果您正苦於以下問題:Java BigDecimal.scale方法的具體用法?Java BigDecimal.scale怎麽用?Java BigDecimal.scale使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.math.BigDecimal的用法示例。


在下文中一共展示了BigDecimal.scale方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: scaledDecimal

import java.math.BigDecimal; //導入方法依賴的package包/類
public static long scaledDecimal(Object a, int scale) {

        if (a == null) {
            return 0;
        }

        if (scale == 0) {
            return 0;
        }

        BigDecimal value = ((BigDecimal) a);

        if (value.scale() == 0) {
            return 0;
        }

        value = value.setScale(0, BigDecimal.ROUND_FLOOR);
        value = ((BigDecimal) a).subtract(value);

        return value.movePointRight(scale).longValue();
    }
 
開發者ID:Julien35,項目名稱:dev-courses,代碼行數:22,代碼來源:NumberType.java

示例2: encodeInternal

import java.math.BigDecimal; //導入方法依賴的package包/類
@Override
ByteBuffer encodeInternal(BigDecimal input) {
  BigInteger bi = input.unscaledValue();
  int scale = input.scale();
  byte[] bibytes = bi.toByteArray();

  ByteBuffer bytes = ByteBuffer.allocate(4 + bibytes.length);
  bytes.putInt(scale);
  bytes.put(bibytes);
  bytes.rewind();
  return bytes;
}
 
開發者ID:datastax,項目名稱:simulacron,代碼行數:13,代碼來源:CqlMapper.java

示例3: write

import java.math.BigDecimal; //導入方法依賴的package包/類
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    SerializeWriter out = serializer.out;

    if (object == null) {
        out.writeNull(SerializerFeature.WriteNullNumberAsZero);
    } else {
        BigDecimal val = (BigDecimal) object;

        String outText;
        if (out.isEnabled(SerializerFeature.WriteBigDecimalAsPlain)) {
            outText = val.toPlainString();
        } else {
            outText = val.toString();
        }
        out.write(outText);

        if (out.isEnabled(SerializerFeature.WriteClassName) && fieldType != BigDecimal.class && val.scale() == 0) {
            out.write('.');
        }
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:22,代碼來源:BigDecimalCodec.java

示例4: decodeText

import java.math.BigDecimal; //導入方法依賴的package包/類
@Override
public Number decodeText(int len, ByteBuf buff) {
  // Todo optimize that
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  BigDecimal big = new BigDecimal(cs.toString());
  // julien : that does not seem consistent to either return a Double or BigInteger
  if (big.scale() == 0) {
    return big.toBigInteger();
  } else {
    // we might loose precision here
    return big.doubleValue();
  }
}
 
開發者ID:vietj,項目名稱:reactive-pg-client,代碼行數:14,代碼來源:DataType.java

示例5: multiplyPrecise

import java.math.BigDecimal; //導入方法依賴的package包/類
public static Number multiplyPrecise(Number op1, Number op2) throws Exception {
	BigDecimal result =  null;
	if(op1 instanceof BigDecimal){
		if(op2 instanceof BigDecimal){
			result = ((BigDecimal)op1).multiply((BigDecimal)op2);
		}else{
			result = ((BigDecimal)op1).multiply(new BigDecimal(op2.toString()));
		}
	}else{
		if(op2 instanceof BigDecimal){
			result = new BigDecimal(op1.toString()).multiply((BigDecimal)op2);
		}else{
			result = new BigDecimal(op1.toString()).multiply(new BigDecimal(op2.toString()));
		}
	}
	if(result.scale() ==0){
		long tempLong =  result.longValue();
		if(tempLong <= Integer.MAX_VALUE && tempLong >= Integer.MIN_VALUE){
			return (int)tempLong;
		}else{
			return tempLong;
		}
	}else{
		return result;
	}
}
 
開發者ID:alibaba,項目名稱:QLExpress,代碼行數:27,代碼來源:OperatorOfNumber.java

示例6: dividePrecise

import java.math.BigDecimal; //導入方法依賴的package包/類
public static Number dividePrecise(Number op1, Number op2) throws Exception {
	BigDecimal result =  null;
	if(op1 instanceof BigDecimal){
		if(op2 instanceof BigDecimal){
			result = ((BigDecimal)op1).divide((BigDecimal)op2, DIVIDE_PRECISION, BigDecimal.ROUND_HALF_UP);
		}else{
			result = ((BigDecimal)op1).divide(new BigDecimal(op2.toString()), DIVIDE_PRECISION, BigDecimal.ROUND_HALF_UP);
		}
	}else{
		if(op2 instanceof BigDecimal){
			result = new BigDecimal(op1.toString()).divide((BigDecimal)op2, DIVIDE_PRECISION, BigDecimal.ROUND_HALF_UP);
		}else{
			result = new BigDecimal(op1.toString()).divide(new BigDecimal(op2.toString()), DIVIDE_PRECISION, BigDecimal.ROUND_HALF_UP);
		}
	}
	if(result.scale() ==0){
		long tempLong =  result.longValue();
		if(tempLong <= Integer.MAX_VALUE && tempLong >= Integer.MIN_VALUE){
			return (int)tempLong;
		}else{
			return tempLong;
		}
	}else{
		return result;
	}
}
 
開發者ID:alibaba,項目名稱:QLExpress,代碼行數:27,代碼來源:OperatorOfNumber.java

示例7: convertCash

import java.math.BigDecimal; //導入方法依賴的package包/類
public static DCashFund convertCash(Value row) {

        DCashFund cashFund = new DCashFund();

        for (Value_ value : row.getValue()) {

            try {

                String methodName = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, value.getName());

                switch (value.getName()) {
                    case "id":
                        int intValue = (int) (double) value.getValue();
                        DCashFund.class.getMethod(methodName, int.class).invoke(cashFund, intValue);
                        break;
                    case "currencyCode":
                        String stringValue = (String) value.getValue();
                        DCashFund.class.getMethod(methodName, String.class).invoke(cashFund, stringValue);
                        break;
                    case "value":
                        BigDecimal bdValue = new BigDecimal((double) value.getValue());
                        if (bdValue.scale() > 4) {
                            bdValue = bdValue.setScale(4, RoundingMode.HALF_UP);
                        }
                        DCashFund.class.getMethod(methodName, BigDecimal.class).invoke(cashFund, bdValue);
                        break;

                }
            } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                DLog.DEGIRO.error("Error while setting value of cash fund", e);
            }

        }
        return cashFund;
    }
 
開發者ID:indiketa,項目名稱:degiro-java-client,代碼行數:36,代碼來源:DUtils.java

示例8: lnValue

import java.math.BigDecimal; //導入方法依賴的package包/類
/**
 * Returns logarithm of the value represented by the instance. Returns NaN if the
 * value is negative.
 */
public double lnValue() {
    //return BigDecimalMath.log(value).doubleValue();
    BigDecimal temp = value.round(outputPrecision);
    int scale = temp.scale();
    double unscaledValue = temp.unscaledValue().doubleValue();
    return Math.log(unscaledValue) - scale * Math.log(10.);
}
 
開發者ID:etomica,項目名稱:etomica,代碼行數:12,代碼來源:MyBigDecimal.java

示例9: convertToTypeLimits

import java.math.BigDecimal; //導入方法依賴的package包/類
/** @todo - review usage to see if range enforcement / java type conversion is necessary */
public Object convertToTypeLimits(SessionInterface session, Object a) {

    if (a == null) {
        return null;
    }

    switch (typeCode) {

        case Types.TINYINT :
        case Types.SQL_SMALLINT :
        case Types.SQL_INTEGER :
        case Types.SQL_BIGINT :
            return a;

        case Types.SQL_REAL :
        case Types.SQL_FLOAT :
        case Types.SQL_DOUBLE :
            return a;

        case Types.SQL_NUMERIC :
        case Types.SQL_DECIMAL : {
            BigDecimal dec = (BigDecimal) a;

            if (scale != dec.scale()) {
                dec = dec.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
            }

            int p = JavaSystem.precision(dec);

            if (p > precision) {
                throw Error.error(ErrorCode.X_22003);
            }

            return dec;
        }
        default :
            throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
}
 
開發者ID:tiweGH,項目名稱:OpenDiabetes,代碼行數:41,代碼來源:NumberType.java

示例10: format

import java.math.BigDecimal; //導入方法依賴的package包/類
@Override
public boolean format(DateTimePrintContext context, StringBuilder buf) {
    Long value = context.getValue(field);
    if (value == null) {
        return false;
    }
    DecimalStyle decimalStyle = context.getDecimalStyle();
    BigDecimal fraction = convertToFraction(value);
    if (fraction.scale() == 0) {  // scale is zero if value is zero
        if (minWidth > 0) {
            if (decimalPoint) {
                buf.append(decimalStyle.getDecimalSeparator());
            }
            for (int i = 0; i < minWidth; i++) {
                buf.append(decimalStyle.getZeroDigit());
            }
        }
    } else {
        int outputScale = Math.min(Math.max(fraction.scale(), minWidth), maxWidth);
        fraction = fraction.setScale(outputScale, RoundingMode.FLOOR);
        String str = fraction.toPlainString().substring(2);
        str = decimalStyle.convertNumberToI18N(str);
        if (decimalPoint) {
            buf.append(decimalStyle.getDecimalSeparator());
        }
        buf.append(str);
    }
    return true;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:30,代碼來源:DateTimeFormatterBuilder.java

示例11: getScaledValue

import java.math.BigDecimal; //導入方法依賴的package包/類
private String getScaledValue(String valueStr, long decimals) {
    // Perform decimal conversion
    BigDecimal value = new BigDecimal(valueStr);
    value = value.divide(new BigDecimal(Math.pow(10, decimals)));
    int scale = 3 - value.precision() + value.scale();
    return value.setScale(scale, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}
 
開發者ID:TrustWallet,項目名稱:trust-wallet-android,代碼行數:8,代碼來源:TransactionDetailActivity.java

示例12: createOptionValue

import java.math.BigDecimal; //導入方法依賴的package包/類
private static OptionValue createOptionValue(final String name, final OptionValue.OptionType type,
                                             final SqlLiteral literal) {
  final Object object = literal.getValue();
  final SqlTypeName typeName = literal.getTypeName();
  switch (typeName) {
  case DECIMAL: {
    final BigDecimal bigDecimal = (BigDecimal) object;
    if (bigDecimal.scale() == 0) {
      return OptionValue.createLong(type, name, bigDecimal.longValue());
    } else {
      return OptionValue.createDouble(type, name, bigDecimal.doubleValue());
    }
  }

  case DOUBLE:
  case FLOAT:
    return OptionValue.createDouble(type, name, ((BigDecimal) object).doubleValue());

  case SMALLINT:
  case TINYINT:
  case BIGINT:
  case INTEGER:
    return OptionValue.createLong(type, name, ((BigDecimal) object).longValue());

  case VARBINARY:
  case VARCHAR:
  case CHAR:
    return OptionValue.createString(type, name, ((NlsString) object).getValue());

  case BOOLEAN:
    return OptionValue.createBoolean(type, name, (Boolean) object);

  default:
    throw UserException.validationError()
      .message("Dremio doesn't support assigning literals of type %s in SET statements.", typeName)
      .build(logger);
  }
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:39,代碼來源:SetOptionHandler.java

示例13: convertProduct

import java.math.BigDecimal; //導入方法依賴的package包/類
public static DPortfolioProduct convertProduct(Value row) {
    DPortfolioProduct productRow = new DPortfolioProduct();

    for (Value_ value : row.getValue()) {

        try {

            String methodName = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, value.getName());

            switch (value.getName()) {
                case "id":
                case "size":
                case "change":
                case "contractSize":
                    long longValue = (long) (double) value.getValue();
                    DPortfolioProduct.class.getMethod(methodName, long.class).invoke(productRow, longValue);
                    break;
                case "product":
                case "currency":
                case "exchangeBriefCode":
                case "productCategory":
                    String stringValue = (String) value.getValue();
                    DPortfolioProduct.class.getMethod(methodName, String.class).invoke(productRow, stringValue);
                    break;
                case "lastUpdate":
                    break;
                case "closedToday":
                case "tradable":
                    Boolean booleanValue = (Boolean) value.getValue();
                    DPortfolioProduct.class.getMethod(methodName, boolean.class).invoke(productRow, booleanValue);
                    break;
                case "price":
                case "value":
                case "closePrice":
                    BigDecimal bdValue = new BigDecimal((double) value.getValue());
                    if (bdValue.scale() > 4) {
                        bdValue = bdValue.setScale(4, RoundingMode.HALF_UP);
                    }
                    DPortfolioProduct.class.getMethod(methodName, BigDecimal.class).invoke(productRow, bdValue);
                    break;

            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            DLog.DEGIRO.error("Error while setting value of portfolio", e);
        }
    }

    return productRow;
}
 
開發者ID:indiketa,項目名稱:degiro-java-client,代碼行數:50,代碼來源:DUtils.java

示例14: fromLogical

import java.math.BigDecimal; //導入方法依賴的package包/類
/**
 * Convert a value from its logical format (BigDecimal) to it's encoded format.
 * @param value the logical value
 * @return the encoded value
 */
public static byte[] fromLogical(Schema schema, BigDecimal value) {
    if (value.scale() != scale(schema))
        throw new DataException("BigDecimal has mismatching scale value for given Decimal schema");
    return value.unscaledValue().toByteArray();
}
 
開發者ID:YMCoding,項目名稱:kafka-0.11.0.0-src-with-comment,代碼行數:11,代碼來源:Decimal.java

示例15: convertToTypeLimits

import java.math.BigDecimal; //導入方法依賴的package包/類
/** @todo - review usage to see if range enforcement / java type conversion is necessary */
@Override
public Object convertToTypeLimits(SessionInterface session, Object a) {

    if (a == null) {
        return null;
    }

    switch (typeCode) {

        case Types.TINYINT :
        case Types.SQL_SMALLINT :
        case Types.SQL_INTEGER :
        case Types.SQL_BIGINT :
            return a;

        case Types.SQL_REAL :
        case Types.SQL_FLOAT :
        case Types.SQL_DOUBLE :
            return a;

        case Types.SQL_NUMERIC :
        case Types.SQL_DECIMAL :
            BigDecimal dec = (BigDecimal) a;

            if (scale != dec.scale()) {
                dec = dec.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
            }

            int valuePrecision = JavaSystem.precision(dec);

            if (valuePrecision > precision) {
                throw Error.error(ErrorCode.X_22003);
            }

            return dec;

        default :
            throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
}
 
開發者ID:s-store,項目名稱:sstore-soft,代碼行數:42,代碼來源:NumberType.java


注:本文中的java.math.BigDecimal.scale方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。