如何在配置单元中使用填充实现舍入到 2 位小数?

How to achieve rounding upto 2 decimals with padding in hive?

下面是我想在配置单元查询本身中解决的问题,而不是在 POJO 中获取数据然后在我的下游程序中应用格式(DefaultFormatter 等)

hive> SELECT ROUND(0.605,2);
0.61
-- Result      = 0.61
-- Expectation = 0.61

hive> SELECT ROUND(0.604, 2);
0.6
--- Result      = 0.6
--- Expectation = 0.60

如何修改 select 构造,使 returns 小数位填充 2 位,而不管它们的值如何。

您可以使用string printf(string format, Obj... args)函数,该函数基于Formatter

select printf('%.2f',0.604);

结果:

0.60

你不需要使用 round():

select printf('%.2f',0.605);

结果:

0.61