本文介绍了为什么MySQL以科学记数法返回一些浮点数,而不是其他的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PHP脚本,通过一个浮点数值的数据库表来查看,这个数值通常有点小,比如:

I'm writing a PHP script that looks through a DB table of float values, that tend to be somewhat small, such as:

0.00052
0.00134
0.00103
0.00149
0.00085
0.00068
0.00077
0.00088
0.00169
0.00063

由于我未知的原因,某些值以科学记数法出现在数据库中,例如:

For reasons unknown to me, some values appear in the DB in scientific notation, such as:

1.12305e-06

1.12305e-06

表被设置为浮动,并且我尝试了PHP中所有的函数强制数字显示为小数,无济于事。尝试我可能,我无法得到这个数字的表格,在任何情况下,始终是小数。

The table is set to float, and I've tried all manner of functions in PHP to force the numbers to display as decimal, to no avail. Try as I might, I'm unable to get this table of numbers to be consistently decimal in all cases.

有关如何解决这个问题的任何建议?已经尝试对(float)进行typcasting,并使用 number_format()和其他几个选项,但每次都不会改变。 / p>

Any suggestions on how to resolve this? Have tried typcasting to (float) and using number_format() and several other options, but no change every time.

推荐答案

如果你坚持使用浮动或双打,你将不得不强迫他们使用像 round(columnName,5)之类的东西来强制显示十进制值。否则,可能会切换到decimnal数据类型。

If you are insisting on using floats or doubles, you will have to force them out using something like round(columnName,5) to force the display in a decimal value. Otherwise, maybe switch to a decimnal data type.

由于浮点值是近似值,并且不是以精确值的形式存储的,因此尝试将它们作为比较对待可能会导致问题。他们也受到平台或实施依赖关系的影响。有关更多信息,请参见第C.5.5.8节

Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section C.5.5.8, "Problems with Floating-Point Values"

另见关于这个确切问题的mysql论坛。

Also see this thread on the mysql forums about this exact issue.

这篇关于为什么MySQL以科学记数法返回一些浮点数,而不是其他的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:21