Jump to content

Archived

This topic is now archived and is closed to further replies.

shadiadiph

[SOLVED] finding the difference between 2 numbers?

Recommended Posts

MM odd question here if I have two numbers that have values such as

 

$current = 1.4567

$last = 1.4698

 

how do i find out the difference between the two numbers sometimes the values might be the otherway round the one above should produce a negative but if $current was 1.4824 the difference would be positive how can i determine this positive negative difference? 

Share this post


Link to post
Share on other sites

$current-$last;

 

Or if you wanted a positive number:

 

abs($current-$last);

Share this post


Link to post
Share on other sites

i figured it out

 

$new =$current-$last gives me a result if negative it shows -2.16 but if the result was positive and +2.16 it only shows 2.16 will your way display it as +2.16 if positive and -2.16 if negative?

Share this post


Link to post
Share on other sites

if($num > 0)

    $num = '+' . $num;

 

 

Note:  When ever you do anything to that number, such as subtract or add something to it, you will need to reprepend the +.

Share this post


Link to post
Share on other sites

Better to use sprintf() to format the number at the point when you need to display it.

 

echo sprintf('%+f',$new);

Share this post


Link to post
Share on other sites

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