#### Archived

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

# [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 on other sites

\$current-\$last;

Or if you wanted a positive number:

abs(\$current-\$last);

##### 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 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 on other sites

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

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

×

• #### Activity

• Chat
×
• Create New...