Removing insignificant decimal characters? [message #169479] |
Tue, 14 September 2010 23:54 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
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;
}
?>
|
|
|
|
|
Re: Removing insignificant decimal characters? [message #169492 is a reply to message #169487] |
Wed, 15 September 2010 09:33 |
Greg Russell
Messages: 2 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
In news:4c90280a$1(at)news(dot)x-privat(dot)org,
MikeB <mpbrede(at)gmail(dot)com> typed:
>>> eg. I'd like the function to print
>>> 300 bytes iso 300.00 bytes
....
>> Use printf().
....
> I'm using sprintf, which accepts the same inputs as printf. What I can't
> grog is how to make the insignificant .00 to not print. The way I read
> the sprintf formatting, I can't see how to do zero-suppression on
> trailing decimal digits.
Someone more versant will no doubt suggest a more concise solution:
<?php
$dvnd = 22;
$dvsr = array(1,2,3,5,7,11);
foreach ($dvsr as $k => $v) {
$result = $dvnd/$v;
if (($dvnd % $v) > 0) {
echo sprintf( "$dvnd/$v = %.4f<br>", $result );
} else {
echo sprintf( "$dvnd/$v = %d<br>", $result );
}
}
?>
produces:
22/1 = 22
22/2 = 11
22/3 = 7.3333
22/5 = 4.4000
22/7 = 3.1429
22/11 = 2
$result = 22/7;
|
|
|
Re: Removing insignificant decimal characters? [message #169505 is a reply to message #169492] |
Wed, 15 September 2010 16:37 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
Member |
|
|
Greg Russell wrote:
> In news:4c90280a$1(at)news(dot)x-privat(dot)org,
> MikeB<mpbrede(at)gmail(dot)com> typed:
>
>>>> eg. I'd like the function to print
>>>> 300 bytes iso 300.00 bytes
> ...
>>> Use printf().
> ...
>> I'm using sprintf, which accepts the same inputs as printf. What I can't
>> grog is how to make the insignificant .00 to not print. The way I read
>> the sprintf formatting, I can't see how to do zero-suppression on
>> trailing decimal digits.
>
> Someone more versant will no doubt suggest a more concise solution:
>
> <?php
> $dvnd = 22;
> $dvsr = array(1,2,3,5,7,11);
>
> foreach ($dvsr as $k => $v) {
> $result = $dvnd/$v;
> if (($dvnd % $v)> 0) {
> echo sprintf( "$dvnd/$v = %.4f<br>", $result );
> } else {
> echo sprintf( "$dvnd/$v = %d<br>", $result );
> }
> }
> ?>
>
> produces:
>
> 22/1 = 22
> 22/2 = 11
> 22/3 = 7.3333
> 22/5 = 4.4000
> 22/7 = 3.1429
> 22/11 = 2
>
> $result = 22/7;
>
>
>
Pretty neat. Thanks.
|
|
|
Re: Removing insignificant decimal characters? [message #169511 is a reply to message #169492] |
Thu, 16 September 2010 02:52 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
Member |
|
|
Greg Russell wrote:
> In news:4c90280a$1(at)news(dot)x-privat(dot)org,
> MikeB<mpbrede(at)gmail(dot)com> typed:
>
>>>> eg. I'd like the function to print
>>>> 300 bytes iso 300.00 bytes
> ...
>>> Use printf().
> ...
>> I'm using sprintf, which accepts the same inputs as printf. What I can't
>> grog is how to make the insignificant .00 to not print. The way I read
>> the sprintf formatting, I can't see how to do zero-suppression on
>> trailing decimal digits.
>
> Someone more versant will no doubt suggest a more concise solution:
>
> <?php
> $dvnd = 22;
> $dvsr = array(1,2,3,5,7,11);
>
> foreach ($dvsr as $k => $v) {
> $result = $dvnd/$v;
> if (($dvnd % $v)> 0) {
> echo sprintf( "$dvnd/$v = %.4f<br>", $result );
> } else {
> echo sprintf( "$dvnd/$v = %d<br>", $result );
> }
> }
> ?>
>
> produces:
>
> 22/1 = 22
> 22/2 = 11
> 22/3 = 7.3333
> 22/5 = 4.4000
> 22/7 = 3.1429
> 22/11 = 2
>
> $result = 22/7;
>
>
>
Greg, just wanted you to know.
I read up the manual (after trying your suggestion) and it turns out
that the modulo divisor (%) converts to integer before doing the
division. I'm not sure why it didn't work for me and apparently worked
for you.
However, I then changed the test to
if ($var > (int) $var)
and that did the trick.
So thanks for getting me on the right track.
M
|
|
|