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

153
Visualizações
How to convert 0.5 to 0.50 in php

Is there a possibility to display "0.5" as "0.50" in php?

I don't mean something like that:

<?php
    $x = 0.5;
    echo($x . "0");
?>

So this can fit in too:

$x = 0.75;

I hope my question is exact enough. Thanks for your help!

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Do:

$x = 0.5;
printf("%.2f", $x);

Get:

0.50

Explanation:

printf() and sprintf() can print/return a formatted string. There are plenty of format parameters available. Here, I used %.2f. Let's take that apart:

  • % denotes a placeholder that will be replaced with $x
  • . denotes a precision specifier
  • 2 belongs to the precision specifier - the number of digits after the decimal point
  • f is the type specifier, here float as that's what we're passing in

Alternatively:

Use number_format():

echo number_format($x, 2);

Here, 2 denotes the number of decimal digits you want in the output. You don't actually need to supply a third and fourth parameter, as their defaults are exactly what you want.

about 4 years ago · Santiago Trujillo Relatório

0

Use number_format() function

echo number_format($x,  2, '.', '');
about 4 years ago · Santiago Trujillo Relatório

0

Use number_format Function

<?php
print number_format('0.5',2,'.','');

// 0.50
?>

http://php.net/manual/en/function.number-format.php

about 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