FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Removing insignificant decimal characters?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Removing insignificant decimal characters? [message #169479] Tue, 14 September 2010 23:54 Go to previous message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma:
Member
I have the following little routing to reduce large numbers into more
comprehensible human-readable values.

What I would like is for some smarts, to not append the decimal or
trailing zeroes in the decimal positions if those are not significant.

eg. I'd like the function to print

300 bytes iso 300.00 bytes

and

4 Kb iso 4.00 Kb.

Any hints as to how I can do this?

Oh, and any comments on coding is appreciated, I'm learning this stuff
and criticism helps. :)

thanks
MikeB

<?php
$mfs = 4096;

echo "<br> Say 4 Kb: " . return_sized($mfs);
echo "<br/>Say 300 bytes: " . return_sized(300);
echo "<br/> Say 5,000 bytes: " . return_sized(5000);
echo "<br/> Say 5,000,000 bytes: " . return_sized(5000000);
echo "<br/> Say 5,000,000,000 bytes: " . return_sized(5000000000);
echo "<br/> Say 5,000,000,000,000 bytes: " . return_sized(5000000000000);

function return_sized($val) {
/*
* return a large number in more human-readable format
*/
$suffix = array('bytes', ' Kb', ' Mb', ' Gb');
for ($i = 0; $i < 3; ++$i) {
if ($val > 1024) {
$val /= 1024;
} else
break;
};
$val = sprintf("%.2f",$val);
// echo "<br/>Function suffix value is $i<br/>";
$val .= $suffix[$i];
// echo '<br/>$val has value: ' . $val ."<br/>" ;
return $val;
}

?>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: getting amount of deleted rows from: $result=odbc_exec($conn, 'delete from....');
Next Topic: echo other way to output a constant?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 12 22:05:53 GMT 2024

Total time taken to generate the page: 0.04655 seconds