Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to compare two variable floats using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 189
    Comment on it

    While making the calculation with float variable one should really careful since there can be three cases, It can < or > or =. So lets see the example below and learn how to get the comparative.

    $varOne = round(0.250, 3);
    varTwo = round(1 - 0.750, 3); //0.250
    if($varOne  == $varTwo  ){
        echo 'varOne and varTwo  are same variable';
    }
    else {
        echo 'varOne and varTwo  are not same variable';
    }
    

    But I'll suggest always make 3 or 4 decimal digits in case of float comparison.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: