使用 postgres,我需要将总分钟数转换为 1h 1m 的格式。
我正在通过以下代码执行此操作
replace(
(119 / 60 + (119 % 60) / 100.0)::text,
'.',
'h '
) + 'm'
返回 1h 59000000000000000000m
119 只是我添加的分钟数的示例,但这将在更大的查询上设置
(accumulated_minutes / 60 + (accumulated_minutes % 60) / 100.0)::text,
有没有办法在不增加额外精度的情况下将其转换为文本?
最佳答案
让数据库为您完成繁重的工作!您可以将分钟数转换为 interval
数据类型,并使用 to_char()
进行格式化:
to_char(make_interval(mins => accumulated_minutes), 'hh24"h" mi"m"')
select to_char(make_interval(mins => 119), 'hh24"h" mi"m"') val
| val | | :------ | | 01h 59m |
If you want to suppress the leading 0
s on hours and minutes, then:
to_char(make_interval(mins => accumulated_minutes), 'fmhh24"h" fmmi"m"') val
关于sql - 将 double 转换为文本的 Postgres 创建 '1.50000000000',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62329572/