|
Posted by Andy Hassall on 10/22/26 11:26
On 11 Sep 2005 18:36:09 -0700, "tencip" <tencip@yahoo.com> wrote:
>So, I'm running data through an array, and having a little difficulty
>in using the number_format function. You see, I'm trying to get a
>comma to appear for the thousandths place, however, whenever i use the
>number_format function, it also forces rounding of the decimal.
>
>Does anyone know how I can place commas in the thousandths place
>WITHOUT doing any decimal rounding? I have some datapoints with two
>decimal places, some with four, and others with none. I don't want to
>have the function affect any of these decimal places.
This is a bit gruesome but it's the first thing that came to mind:
<?php
function number_format_keep_decimals($num)
{
if ($point = strrpos($num, '.'))
{
return number_format($num, strlen($num) - $point - 1);
}
else
{
return number_format($num);
}
}
$nums = array(1000.2, 2000.34, 5000.678, 9000);
foreach ($nums as $num)
{
print $num . ' -> ' .number_format_keep_decimals($num);
print "<br>";
}
?>
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Navigation:
[Reply to this message]
|