简体   繁体   中英

Php weird rounding

Simple question: Why this behavior:

var_dump($calc_product);
var_dump($calc_price);
var_dump(round($calc_product,1));
var_dump(round($calc_price,1));

gives:

float(16.05) float(16.05) float(16) float(16.1)

Thanks!

Don't use floats for money.

A common method is to store the price in pence/cents, then to divide by 100 on display. This means you can use integer arithmetic instead, and avoids this issue.

Read

number_format and

money_format ...

sure it will helps you. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM