如何在php中使用MySQL十进制数据类型?

时间:2022-05-06 16:32:39

Hopefully just a quick question. I have a DECIMAL column in my database. The value is a very small decimal fraction - summing this value for all rows would equal 1.

希望能有个简单的问题。我的数据库中有一个十进制的列。这个值是一个非常小的小数——将所有行的这个值相加等于1。

Now I'd like to use this value in my php application, display it, perform calculations upon it and save it back to the database.

现在我想在php应用程序中使用这个值,显示它,对它执行计算并将其保存到数据库中。

As php only has integer and float types, what is the best way to use that value in php so as not to lose any precision in calculations or display?

由于php只有整数和浮点类型,那么如何在php中使用该值以避免在计算或显示中丢失任何精度呢?

  • Keep the value as a string and use BC Math for calculations
  • 将值保存为字符串,并使用BC Math进行计算
  • Cast the number as float - I know php floats are precise to a good number (depending on the OS)
  • 将数字转换为浮点数——我知道php浮点数精确到一个好数字(取决于操作系统)
  • Convert the value to an integer using a function which remembers the exponent
  • 使用记住指数的函数将值转换为整数。
  • Something else?
  • 别的吗?

Thanks

谢谢

1 个解决方案

#1


0  

You should go for float to not lose precision in calculation and use number_format output for displaying...

您应该使用float来避免在计算中丢失精度,并使用number_format输出来显示……

Notes:
It's really depend on your needs and how you want your output handled, if you wish just cutting the mantissa after second number or do some kind of rounding.

注意:这取决于您的需要和您希望如何处理输出,如果您希望在第二个数字之后削减尾数或做某种舍入。

With usage of BC Math with scale 2 for example 2.10/1.10 produce 1.90 while using number_format(2.10/1.10, 2) will result in 1.91 (rounding the results just like SELECT CAST(2.10/1.10 as DECIMAL(10,2)))

使用BC数学并使用scale 2,例如2.10/1.10产生1.90,而使用number_format(2.10/1.10, 2)将产生1.91(四舍五入结果就像选择CAST(2.10/1.10作为DECIMAL(10,2))))

Update:. As stated in comments there may be cases you will lose precision in calculations. It will be probably better to do arbitrary-precision calculations in MySQL itself so you can be sure nothing is loosed during math operations.

更新:。如注释所述,可能会在计算中失去精度。最好在MySQL中进行任意精度的计算,这样您就可以确保在数学操作过程中不会出现任何问题。

#1


0  

You should go for float to not lose precision in calculation and use number_format output for displaying...

您应该使用float来避免在计算中丢失精度,并使用number_format输出来显示……

Notes:
It's really depend on your needs and how you want your output handled, if you wish just cutting the mantissa after second number or do some kind of rounding.

注意:这取决于您的需要和您希望如何处理输出,如果您希望在第二个数字之后削减尾数或做某种舍入。

With usage of BC Math with scale 2 for example 2.10/1.10 produce 1.90 while using number_format(2.10/1.10, 2) will result in 1.91 (rounding the results just like SELECT CAST(2.10/1.10 as DECIMAL(10,2)))

使用BC数学并使用scale 2,例如2.10/1.10产生1.90,而使用number_format(2.10/1.10, 2)将产生1.91(四舍五入结果就像选择CAST(2.10/1.10作为DECIMAL(10,2))))

Update:. As stated in comments there may be cases you will lose precision in calculations. It will be probably better to do arbitrary-precision calculations in MySQL itself so you can be sure nothing is loosed during math operations.

更新:。如注释所述,可能会在计算中失去精度。最好在MySQL中进行任意精度的计算,这样您就可以确保在数学操作过程中不会出现任何问题。