Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report

0

Use number_format() function

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!