[BUG] Fix potential overflow exception when do money format for double (#6408)

* [BUG] Fix potential overflow bug when do money format for double

Co-authored-by: caiconghui <caiconghui@xiaomi.com>
This commit is contained in:
caiconghui
2021-08-15 18:40:26 +08:00
committed by GitHub
parent 2030c44dba
commit 285d44cd48
3 changed files with 63 additions and 39 deletions

View File

@ -879,11 +879,8 @@ StringVal StringFunctions::money_format(FunctionContext* context, const DoubleVa
if (v.is_null) {
return StringVal::null();
}
double v_cent = MathFunctions::my_double_round(v.val, 2, false, false);
bool negative = v_cent < 0;
int32_t frac_value = negative ? ((int64_t) (-v_cent * 100)) % 100 : ((int64_t)(v_cent * 100)) % 100;
return do_money_format<int64_t, 26>(context, (int64_t) v_cent , frac_value);
return do_money_format(context, fmt::format("{:.2f}", v_cent));
}
StringVal StringFunctions::money_format(FunctionContext* context, const DecimalV2Val& v) {
@ -893,7 +890,7 @@ StringVal StringFunctions::money_format(FunctionContext* context, const DecimalV
DecimalV2Value rounded(0);
DecimalV2Value::from_decimal_val(v).round(&rounded, 2, HALF_UP);
return do_money_format<int64_t, 26>(context, rounded.int_value(), abs(rounded.frac_value()));
return do_money_format<int64_t, 26>(context, rounded.int_value(), abs(rounded.frac_value() / 10000000));
}
StringVal StringFunctions::money_format(FunctionContext* context, const BigIntVal& v) {