Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

349
Visualizações
Postgres converting double double precision to text creates '1.50000000000'

Working with postgres and I need to convert total minutes into the format of 1h 1m.

I'm doing this via the following code

replace(
    (119 / 60 + (119 % 60) / 100.0)::text, 
    '.', 
    'h '
) + 'm'

which returns 1h 59000000000000000000m

119 is just an example amount of minutes I've added, but this would be set on a larger query that would do

(accumulated_minutes / 60 + (accumulated_minutes % 60) / 100.0)::text, 

is there a way to convert this to text without adding the extra precision?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Let the database do the heavy lifiting for you! You can turn the number of minutes to an interval datatype, and use to_char() for formating:

to_char(make_interval(mins => accumulated_minutes), 'hh24"h" mi"m"')

Demo on DB Fiddle:

select to_char(make_interval(mins => 119), 'hh24"h" mi"m"') val
| val     |
| :------ |
| 01h 59m |

If you want to suppress the leading 0s on hours and minutes, then:

to_char(make_interval(mins => accumulated_minutes), 'fmhh24"h" fmmi"m"') val
over 4 years ago · Santiago Trujillo Relatório

0

You may try first casting the quotient or modulus to integer, then casting to text after that, e.g.

SELECT (119 / 60)::int::text || 'h ' || (119 % 60)::int::text || 'm';

This outputs 1h 59m.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda