I am rounding values to 4 digits
using round($num,4) so i get values like
7.456822 to 7.4568 And sometimes i get values like 2.3 or 6 i want to fill the rest with zeros so they look like this
2.3000
6.0000
How do i get this?
Thanks in advance
Quick Question
Moderator: General Moderators
This should work for you...
Code: Select all
<?php
$num = 6.2;
switch ($num) {
case is_integer($num):
$num .= '.';
$num = str_pad($num, 6, '0');
break;
case is_float($num):
$num = str_pad($num, 6, '0');
break;
default:
$num = 'Error!';
}
echo $num;
?>-
AngryPanda
- Forum Newbie
- Posts: 16
- Joined: Wed Jul 19, 2006 12:18 am
That does typecast to string though. Whether or not that is an issue I do not know.
Perhaps number_format() would be a better solution for this.
Perhaps number_format() would be a better solution for this.
Code: Select all
$num = 6.45998445;
$rounded = round($num, 4);
$formatted = number_format($rounded, 4);use http://php.net/sprintf in combination with (float) to typecast it.