Page 1 of 1
Quick Question
Posted: Fri Jul 21, 2006 1:06 am
by imatrox05
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
Posted: Fri Jul 21, 2006 1:10 am
by feyd
strpos() to see if it has a decimal,
str_pad() to add zeros.
Posted: Fri Jul 21, 2006 5:18 am
by imatrox05
Thanks for the tip but that doesnt seem to work. I am getting 1000 for all my results
Posted: Fri Jul 21, 2006 5:37 am
by Benjamin
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;
?>
Posted: Fri Jul 21, 2006 5:56 am
by AngryPanda
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.
Code: Select all
$num = 6.45998445;
$rounded = round($num, 4);
$formatted = number_format($rounded, 4);
Posted: Fri Jul 21, 2006 6:04 am
by Benjamin
Yeah that would work better..
Posted: Fri Jul 21, 2006 6:20 am
by patrikG
use
http://php.net/sprintf in combination with (float) to typecast it.