0
$amount = (256.34 * 100);
echo (int) ($dollor_amount);

// out should be 25634 but this giving 25633, not sure why?


$amount = (255.34 * 100);
echo (int) ($dollor_amount);
// For 255.34 it returning fine output 25534

is there any limit issue? it behaving unexpected only after 256 number.

  • See [this SO topic](https://stackoverflow.com/questions/14082287/why-are-floating-point-numbers-printed-so-differently). Basically, you can never rely 100% on floating point numbers. Most likely here `256.34 * 100` is in fact `256.33999999999999*100`, so you get `25633.99999`, and when typecasted to int, the decimals are just cut right off. – Qirel Oct 08 '17 at 10:25
  • 1
    What I am wondering is why [when you are not casting it to an int](https://tio.run/##K8go@P/fxr4go4BLJTE3vzSvRMFWQcPI1EzP2ERBS8HQwEDTmis1OSNfASoN5SnFFMXkKUE5Gpl5JZoKGlAVmmhKUA02pchge7v//wE), it _is_ printing the expected result. – Ivar Oct 08 '17 at 10:29
  • 1
    It has to do with how floating-point arithmetic is performed (and the fact that `(int)3.14` becomes `3`, regardless of how close to the upper integer the decimal is), which can never be relied on. [Here is another thread on SO](https://stackoverflow.com/questions/3726721/php-floating-number-precision) which might be more informative. – Qirel Oct 08 '17 at 10:31

1 Answers1

-1

Used float instead of int. you will get your desired result as par your question.

$amount = (256.34 * 100);
echo (float) ($amount);

// out should be 25634 but this giving 25633, not sure why?

$amount = (255.34 * 100);
echo (float) ($amount);
sheraz
  • 434
  • 4
  • 8