Float converted into plastic that something

We all know that a float is not very accurate, so when carrying out the type of transformation will be a lot less than the issue of intent

I use php for everyone to talk about this matter with the code

$a = 2.01;
var_dump(sprintf('%.20F', $a * 100));//string(24) "200.99999999999997157829"
var_dump( intval( $a * 100) );//int(200)

The above results are surprising, right, 2.01 * 100 201 it should be, actually 200.99999999 .... so that you can explain why later transformed into shaping up to become 200

How to solve this problem? Float => int before the first round use

$a = 2.01;
var_dump(sprintf('%.20F', $a * 100));//string(24) "200.99999999999997157829"
var_dump( intval( round($a * 100) ) );//int(201)

In fact, most cases do not have too much to consider this issue, but in the payment system, we must consider this issue, because commodity prices are generally required to pay points, that is, to above 2.01 points. This time if the order data in the table is stored in the unit price points have a problem, according to the above examples is likely to keep commodity is 200,210, a final settlement has been 200.



The ultimate solution to use mysql precision required by the type of decimal



Original Address: float transformed into something that the plastic
tag: float    int   

Intelligent Recommendation

Reproduced in: https: //my.oschina.net/54php/blog/600293

Guess you like

Origin blog.csdn.net/weixin_34023863/article/details/91634685