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
Re: Removing insignificant decimal characters? [message #169492 is a reply to message #169487] Wed, 15 September 2010 09:33 Go to previous messageGo to previous message
Greg Russell is currently offline  Greg Russell
Messages: 2
Registered: September 2010
Karma:
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;
[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: Sat Nov 23 06:55:48 GMT 2024

Total time taken to generate the page: 0.04493 seconds