本文整理汇总了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();
}
示例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;
}
示例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('.');
}
}
}
示例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();
}
}
示例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;
}
}
示例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;
}
}
示例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;
}
示例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.);
}
示例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");
}
}
示例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;
}
示例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();
}
示例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);
}
}
示例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;
}
示例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();
}
示例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");
}
}