Reply to Re: Using number_format without changing decimal

Your name:

Reply:


Posted by Andy Hassall on 10/05/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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация