如何存储百分比值? [英] How to store a percentage value?

查看:1441
本文介绍了如何存储百分比值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails 3.2.2,我想在我的数据库表列中存储处理百分比值。你建议什么(例如,什么类型 - :integer :float :decimal ,... - 我应该设置)?是否有一些警告,我必须/应该知道存储百分比值?

I am using Rails 3.2.2 and I would like to store and handle a percentage value in a my database table column. What do you advice about (for example, what type - :integer, :float, :decimal, ... - of the column should I set)? Is there some alert that I must/should know about storing percentage values?

精度应为 2 1.99 % 50.01%,...)。

The precision should be 2 (1.99%, 50.01%, ...).

/ em>:我阅读了此帖

Note: I read this post.

推荐答案

这取决于你如何计划使用该值,但通常 decimal 一个很好的选择存储百分比。请注意,您可以将其存储为百分比值(10.01)或小数(.1001)。因此,这将影响列的实际大小和精度。

It depends a little on how you plan to use the value, but typically a decimal would be a good choice for storing percentages. And note that you could store it as the percentage value (10.01) or as the fractional (.1001). So that would affect the actual size and precision of the column.

存储为百分比或分数之间的选择取决于使用需求,但我 / em>在大多数情况下,将其更简单地存储为一个分数(例如,.10表示10%),因为它可以更容易地直接在大多数计算中使用(不需要记住除以100) 。

The choice between storing as a percent or fraction depends on the usage needs, but I suspect that in most situations it would be simpler to store it as a fraction (e.g., .10 to represent 10%) because it can be used directly in most calculations more easily (don't need to remember to divide by 100).

正如@ismaelga在评论中指出的,十进制是精确度的一个好选择(特别是在处理货币计算时非常好)。

And as @ismaelga points out in the comments, a decimal is a good choice for accuracy (particularly nice when dealing with monetary calculations).

这篇关于如何存储百分比值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆