moiisam10 Posted August 17, 2015 Share Posted August 17, 2015 (edited) Hello guys... Am here again maybe with a noob question.. but well am just triying to learn all possible. I got a variable... $lal = 4820.492 and i need to divie all in 2... like $lal2 = 2410.246 I triyied with $lal2 = $lal/2; echo $lal2; But i just get the first character. so its "2" , i triyed with explode but dont work or dont know, with number_format and i dont know how to make it work.. Thanks in advanced to all. Edited August 17, 2015 by moiisam10 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 17, 2015 Share Posted August 17, 2015 (edited) I get your expected result 2410.246 $lal = 4820.492; $lal2 = $lal/2; echo $lal2; // output 2410.246 Are your sure $lal is what you expect it is? What does var_dump($lal); return? Edited August 17, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
moiisam10 Posted August 17, 2015 Author Share Posted August 17, 2015 I get your expected result 2410.246 $lal = 4820.492; $lal2 = $lal/2; echo $lal2; // output 2410.246 Are your sure $lal is what you expect it is? What does var_dump($lal); return? Well... Really i get the $lal with this. $lal = $row[0]->nodeValue; from curl. Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted August 17, 2015 Solution Share Posted August 17, 2015 (edited) Oh. This is from yesterday? You cannot do math operations with numbers which are formatted. You need to remove the number formatting so its whole number // remove number formatting $lal = str_replace(',', '', $row[0]->nodeValue); // now divide by 2 $lal2 = floatval($lal) / 2; echo $lal2; If you dont remove the formatting PHP will truncate the value before the comma, so only 4 will be divided by 2 this why the result is 2 Edited August 17, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
moiisam10 Posted August 17, 2015 Author Share Posted August 17, 2015 Oh. This is from yesterday? You cannot do math operations with numbers which are formatted. You need to remove the number formatting so its whole number // remove number formatting $lal = str_replace(',', '', $row[0]->nodeValue); // now divide by 2 $lal2 = floatval($lal) / 2; echo $lal2; If you dont remove the formatting PHP will truncate the value before the comma, so only 4 will be divided by 2 this why the result is 2 Thanks again bro , i look like a real noob.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.