Hi,All:
使用tableEnv.createTemporaryView(String path, DataStream<T> dataStream,
Schema
schema)创建临时表后使用SQL查询(schema中有个字段为decimal,定义为DataTypes.DECIMAL(10,2))时。当我发送的数据小数位超过2,结果会被四舍五入,如,10.136
-> 10.14。 后面debug时发现在数据转换时,会被四舍五入。
想咨询下:
1、为什么要用RoundingMode.HALF_UP,是出于什么考虑?
2、可以调整成RoundingMode.ROUND_DOWN只取固定的小数位吗?
附录:
DecimalData转换源码
/**
* Creates an instance of {@link DecimalData} from a {@link BigDecimal} and the
given precision
* and scale.
*
* <p>The returned decimal value may be rounded to have the desired scale. The
precision will be
* checked. If the precision overflows, null will be returned.
*/
public static @Nullable DecimalData fromBigDecimal(BigDecimal bd, int
precision, int scale) {
bd = bd.setScale(scale, RoundingMode.HALF_UP);
if (bd.precision() > precision) {
return null;
}
long longVal = -1;
if (precision <= MAX_COMPACT_PRECISION) {
longVal = bd.movePointRight(scale).longValueExact();
}
return new DecimalData(precision, scale, longVal, bd);
}