Lost data when convert float to int in php

前端 未结 4 799
执念已碎
执念已碎 2020-12-12 01:55

My code :

$baseprice=1705000/1.1;
var_dump($baseprice);
$parseprice=intval($baseprice);
var_dump($parseprice);

Result :

f         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 02:24

    You are using floating point numbers which will lose precision (see this link).

    If you are handling currency use integers and work in cents, pence, pfennigs, or whatever. Convert to a currency format just before display.

提交回复
热议问题