Removing all decimals in PHP

前端 未结 10 1995
囚心锁ツ
囚心锁ツ 2020-12-16 12:27

get this from my database:

252.587254564

Well i wanna remove the .587254564 and keep the 252, how can i do that?

相关标签:
10条回答
  • 2020-12-16 12:27

    In PHP you would use:

    $value = floor($value);
    

    floor: Returns the next lowest integer value by rounding the value down if necessary.

    If you wanted to round up it would be:

    $value = ceil($value);
    

    ceil: Returns the next highest integer value by rounding the value up if necessary.

    0 讨论(0)
  • 2020-12-16 12:27

    As Tricker mentioned you can round the value down or you can just cast it to int like so:

    $variable = 252.587254564; // this is of type double
    $variable = (int)$variable; // this will cast the type from double to int causing it to strip the floating point.
    
    0 讨论(0)
  • 2020-12-16 12:29

    You can do a simply cast to int.

    $var = 252.587254564;
    $var = (int)$var; // 252
    
    0 讨论(0)
  • 2020-12-16 12:29

    Before using above answer what is your exact requirement please see bellow example output.

    $val = 252.587254564;
    
    echo (int)$val; //252
    echo round($val, 0); //253
    echo ceil($val); //253
    
    
    $val = 1234567890123456789.512345;
    
    echo (int)$val; //1234567890123456768
    echo round($val, 0);//1.2345678901235E+18
    echo ceil($val); //1.2345678901235E+18
    
    
    $val = 123456789012345678912.512345;
    
    echo (int)$val; //-5670419503621177344
    echo round($val, 0);//1.2345678901235E+20
    echo ceil($val); //1.2345678901235E+20
    
    0 讨论(0)
  • 2020-12-16 12:38

    Convert the float number to string, and use intval to convert it to integer will give you 1990

    intval(("19.90"*100).'')
    
    0 讨论(0)
  • 2020-12-16 12:41

    And there is also a not quite advisable method:

    strtok($value, ".");
    

    This cuts of the first part until it encounters a dot. The result will be a string, not a PHP integer. While it doesn't affect using the result much, it's not the best option.

    0 讨论(0)
提交回复
热议问题