简体   繁体   English

如何存储长度,内存,重量之类的数据而又不丢失单位的重要性并能够比较两个值?

[英]How to store data like length, memory, weight without losing the significance of the unit and be able to compare two values?

Say I have to populate database fields of the likes of Memory size where in a numerical value with a unit is supposed to be saved. 假设我必须填充“内存大小”之类的数据库字段,其中应该保存带有单位的数值。 It would have been possible to hardcode the logic needed for validating the data entered by the user, had this been the only field that I needed to take care of at one particular place alone in the project. 如果这是我需要在项目中一个特定位置单独处理的唯一字段,则可以对验证用户输入的数据所需的逻辑进行硬编码。 But that not being the case it sounds quite impractical and foolish even think about embedding information of the likes of 1 MB = 1000 KB in the source code. 但是,事实并非如此,即使考虑将1 MB = 1000 KB之类的信息嵌入源代码中,也听起来是不切实际和愚蠢的。 I need to keep such info in the product in order to be able to compare the memory size field of two different entities. 我需要在产品中保留此类信息,以便能够比较两个不同实体的内存大小字段。

Question: What is the ideal way to go about storing scalar data with a unit in a system such that the unit without degrading the unit to a meaning less string? 问题: 在系统中用一个单元存储标量数据而不使该单元降级为较少含义的字符串的理想方法是什么? And can be used in comparison operations? 并且可以用于比较运算吗?

EDIT: Data is coming in from sources I do not control, eg spreadsheets exported by third-parties or data scraped off web pages etc. The units there may be any thing. 编辑:数据来自我无法控制的来源,例如,第三方导出的电子表格或从网页上抓取的数据等。单位可能是任何东西。 Hope it makes the question clear. 希望它能使问题清楚。

In this situation I would normalise the input to a consistent unit. 在这种情况下,我会将输入标准化为一致的单位。 So in your example, you would normalise to bytes, or perhaps megabytes. 因此,在您的示例中,您将标准化为字节或兆字节。

If you had two columns and stored both the numeric value and the units the database will be much harder to query. 如果您有两列并存储了数值和单位,则数据库将更难查询。 For example, a query for "who has more than 10 gigs of storage" will involve inline calculations to handle all the possible units. 例如,查询“谁拥有超过10个存储空间”将涉及内联计算以处理所有可能的单位。 This will prove difficult to index compared to a column expressed in standard units. 与以标准单位表示的列相比,这将很难索引。

If a user enters a figure in gigabytes you probably would not want to display them back as a large number bytes. 如果用户输入的数字以GB为单位,则您可能不希望将其显示为大字节。 This is a user-interface concern - you would write something to display the number of bytes in the most useful unit. 这是一个用户界面问题-您将编写一些内容以最有用的单位显示字节数。

If you strictly need to display values using the same units as entered, then add a "displayUnits" column alongside the number of bytes. 如果严格需要使用与输入相同的单位显示值,请在字节数旁边添加“ displayUnits”列。 Use this in the user interface. 在用户界面中使用它。

I wrote an answer for something similar here Custom unit conversion 我在这里写了一个类似答案的答案自定义单位转换

Basically, you want to store a "Unit of Measure" along with your measure. 基本上,您希望将“度量单位”与度量一起存储。 For example, you can have the value "100" in the value column, and "KB" in the unit_of_measure column. 例如,您可以在值列中拥有值“ 100”,在unit_of_measure列中拥有“ KB”。 If you need to compare this with another row that has "100" and "MB" you need to convert them to a common measure first. 如果需要将其与另一行具有“ 100”和“ MB”的行进行比较,则需要先将它们转换为通用度量。 This can be done with the conversion table I showed in the other answer. 这可以通过我在另一个答案中显示的转换表来完成。

If at all possible, you should strive to normalize the measures before inserting the data in table. 如果有可能,您应在将数据插入表中之前努力对这些度量进行标准化。 This is better from most perspectives. 从大多数角度来看,这更好。 Failing that, I think using a conversion table is the way to go. 失败的话,我认为使用转换表是可行的方法。

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

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