Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

Use number_format() function

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda