简体   繁体   中英

mysql,How to save rounded numbers into longtext field?

I have done this calculation:

$number = round(count($exc)/ count($grades),2,PHP_ROUND_HALF_UP);

When save,I have to save it into a field of an existing table which is a longtext type. The numbers got saved but become very long, something like 0.200000000000000011102230246251565404236316680908203125 I think this might cause extra db storage. When I query the numbers, they are still rounded as 2 digital. So, that's an extra converting job. I would like to learn how to save those numbers as rounded number?

You can use PHP sprintf function coupled with format specifier like so:

$number = sprintf("%.3f", round(count($exc)/ count($grades),2,PHP_ROUND_HALF_UP));

If you need more info about that take a look at sprintf PHP documentation here http://it2.php.net/manual/en/function.sprintf.php .

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