Jump to content

Divide?


moiisam10
Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by moiisam10
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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 by Ch0cu3r
Link to comment
Share on other sites

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.. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.