简体   繁体   English

mysql,如何将四舍五入的数字保存到longtext字段中?

[英]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. 数字已保存但变得很长,例如0.200000000000000011102230246251565404236316680908203125,我认为这可能会导致额外的数据库存储。 When I query the numbers, they are still rounded as 2 digital. 当我查询数字时,它们仍然四舍五入为2个数字。 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: 您可以将PHP sprintf函数与格式说明符结合使用,如下所示:

$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 . 如果您需要更多有关此方面的信息,请在http://it2.php.net/manual/en/function.sprintf.php上查看sprintf PHP文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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