'PHP format number to money format. Decimal before last 2 digits
I need to format this to money format but not sure how. Can someone help
<?php
$input = "40025";
echo $input;
I need to output 400.25
Solution 1:[1]
You could do this for example:
$input = 40025;
$output = $input/100;
$output = number_format($output, 2);
echo $output; // 400,25
Solution 2:[2]
As an alternative to number_format, you could also try money_format and specify a decimal before last two digits. USD example:
$input = "40025";
setlocale(LC_MONETARY, 'en_US.UTF-8');
echo money_format('%.2n', $input);
Solution 3:[3]
You can format it with sprintf
printf("%.2f" 40025/100);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ezequiel Fernandez |
| Solution 2 | Chris McGlade |
| Solution 3 |
