On Sat, 14 Nov 2026 at 02:28, Tom Lane <[email protected]> wrote: > > =?UTF-8?B?6Z+p5bu65qGl?= <[email protected]> writes: > > While working with PG, I found that to_char returns value that is > > right but the format is wrong: > > SELECT to_char(999::numeric, '9.9EEEE'); > > Result is: 10.0e+02, and I think the expected output should be: 1.0e+03. > > With help from ChatGPT, I figured that get_str_from_var_sci() > > determines the exponent > > before rounding the significand. If rounding increases the > > significand's absolute value to > > 10, the original exponent is still used. The attached patch adds a > > check for this,and adjusts > > the significand and the exponent. > > Good point. The patch feels a little brute-force, but I don't see > a simple way to do better, and it's probably not worth a huge > amount of trouble to make this edge case faster. > > Is there any case in which we have the reverse problem, ie rounding > down causes the leading digit to become 0? > > regards, tom lane
Thanks for the review. The patch handles the tmp_var calculated by following two function calls: power_ten_int(exponent, &tmp_var); // rscale is always >= 0 // for a non-zero var: var / 10^exponent is in [1, 10), with round=true the // range is [1, 10] ie. 9.99 will be rounded to 10.0 div_var(var, &tmp_var, &tmp_var, rscale, true, true); And the patch increases exponent only when tmp_var.digits[0] >= 10. So for a non-zero input the tmp_var.digits[0] within range [1, 10] will not be rounded to zero, and I cannot think any other type of reverse problems will occur here. Regards, Jianqiao Han
