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 #169511 is a reply to message #169492] Thu, 16 September 2010 02:52 Go to previous message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma:
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
[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: Wed Nov 27 13:08:18 GMT 2024

Total time taken to generate the page: 0.04206 seconds