Board index   FAQ   Search  
Register  Login
Board index PHP Your Scripts

Modulus (%) operator behave awkwardly!

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

Modulus (%) operator behave awkwardly!

Postby HabibRahman » Sun Feb 10, 2013 11:28 am

dear Members...

see the sample code:

$val = .815;
$val = $val * 10000; // $val should be 8150
$a = $val % 50; // $a should be 0
echo 'remainder: '. $a ;

-------- Output ----
remainder: 49

how ?????? :?

But if I simply write this...
$a = 8150 % 50;

then the result shows 0.

really awkward !!!
HabibRahman
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Feb 10, 2013 11:11 am

Re: Modulus (%) operator behave awkwardly!

Postby seandisanti » Mon Mar 04, 2013 1:01 pm

The reason this happens is a little more complicated than i currently have time to explain, but if you explicitly round it though, it works.

Code: Select all

<?php
$val = .815;
$val = round($val * 10000); // $val should be 8150
echo $val . '<br />';
$a = $val % 50; // $a should be 0
echo 'remainder: '. $a ;
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 787
Joined: Mon Oct 01, 2012 12:32 pm


Return to Your Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron