如何在 MySQL 中将 1/3 存储为小数

发布于 2025-01-07 10:18:20 字数 132 浏览 8 评论 0原文

我试图弄清楚如何在 MySQL 中存储 1/3 或任何导致无限重复十进制值的分数。我不能只使用 3.333333,因为它显然不等于 100。我一直在阅读有关 float 数据类型的内容,但我不确定这是否有效。任何帮助将不胜感激。

谢谢

I am trying to figure out how to store 1/3, or any fraction which results in an infinitely repeating decimal value in MySQL. I cannot just use 3.333333 because it obviously does not total to 100. I have been reading about the float datatype but i'm not sure if this will work. Any help would be appreciated.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2025-01-14 10:18:20

您可以将所有有理数(包括整数)表示为“分子”和“分母”。因此,在您的表中,您将有一个分子和分母列,并且您的应用程序将具有使用该形式存储数字的逻辑。

您仍然无法使用这种技术精确地存储无理数(即,如果您想存储 Pi,无论如何您都需要分数近似)。

请参阅此处了解什么是有理数,以便您了解此技术的局限性。

http://en.wikipedia.org/wiki/Rational_number

You could potentially represent all rational numbers (including integers) as "numerator" and "denominator". So in your table you'd have a numerator and denominator columns, and your app would have logic to store numbers using that form.

You would still be unable to store irrational numbers precisely with this technique (i.e. if you want to store Pi, you'd need a fractional approximation anyway).

See here for what rational numbers are, so you can understand the limitations of this technique.

http://en.wikipedia.org/wiki/Rational_number

梦开始←不甜 2025-01-14 10:18:20

将分子和分母存储在单独的列中。真的就是这么简单。当您想要将所有分数相加时,真正的问题才出现。有些语言有内置的工具可以做到这一点,但我认为 MySQL 没有。

Store the numerator and the denominator in separate columns. It's really that simple. The real problem comes later when you want to add up all the fractions. Some languages have built-in facilities to do so, but I don't think MySQL does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文