Jump to content

[SOLVED] Help number format problem..


chaiwei

Recommended Posts

Hi all,

 

I got 2 variable $td and $tc which get the value from the database

 

if(row['side']=='d')
$td+=row['amount'];

else if (row['side']=='c')
$tc+=row['amount'];

echo $td;   // 3600.29
echo $tc;   // 3600.29

if(($td-$tc)<>0){
echo ($td-$tc);   //4.54747350886E-13
}

By right it should not print any value, right?

but where is the 4.54747350886E-13 came from?

 

but if I put

echo number_format($td-$tc);  //0

It shows me 0

 

what is happening behind there?

 

Link to comment
Share on other sites

The common solution is usually to check if x - y < z where z is a float of arbitrary value, and 1 or both of x and y are floats.

 

 

Example:

 

<?php
$x = 5.34;
$y = 5.34;
if($x - $y < .0000001) {
    "x - y is 0!";
}

 

 

Edit: If the larger number is the one being subtracted, the absolute value must be taken.

Link to comment
Share on other sites

if(($td-$tc) != 0){
echo ($td-$tc);   4.54747350886E-13
}

 

same. also is 4.54747350886E-13

I see,

I try to

echo number_format($tc,50);

it shows me 3,600.28999999999950887286104261875152587890625

which is near to 3600.29

So that's why $td-$tc shows me that 4.54747350886E-13

 

Thanks you guys for helping me out.

 

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.