如何为单位转换表建模?
我正在寻找创建各种单元及其相互关系的数据库模型。例如,36 英寸 = 3 英尺 = 1 码 = 0.9144 米等。该表还可以存储以盎司、磅、千克、克、厘米和各种测量单位表示的杯子。
你如何做到这一点?我在想这样的事情:
Amount | Units | ConversionFactor | ConversionUnits 1 | foot | 12 | inches 1 | yard | 36 | inches
但坦率地说,这似乎是一个糟糕的主意。试图计算出一码有多少英尺会非常复杂,而且我认为我无法存储我需要的所有转换。
还有哪些其他想法?我知道这是一个已解决的问题。谢谢!
I'm looking to create a db model of various units and their relation to each other. For instance, 36 inches = 3 feet = 1 yard = .9144 meters etc. This table would also store cups in ounces, pounds, kg, grams, cm and all sorts of measurements.
How do you do this? I was thinking about something like this:
Amount | Units | ConversionFactor | ConversionUnits 1 | foot | 12 | inches 1 | yard | 36 | inches
But frankly, this seems like a terrible idea. Trying to figure out how many feet in a yard would be very convoluted, and I don't think I could ever store all the conversions I need.
What other ideas are there? I know this is a solved problem. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
存储转换为 SI 单位,而不是其他非公制单位。然后您可以在单位之间进行转换,而无需知道显式转换。
所以 14 英尺换算为:
Store conversions to SI units, not to other non-metric units. Then you can convert between units in without needing to know the explicit conversion.
So 14 feet in yards is:
为您感兴趣的每个维度选择一个基本单位(阅读该维基页面,它将是有用)。例如,如果大部分数据采用 SI 单位,则您可以选择千克表示质量、秒表示时间、米表示距离等。如果您的大部分数据采用美国单位,请从美国习惯单位中选择单位,例如英镑质量,英尺,长度,秒,时间。
然后,对于您希望能够处理的每个实际单位,将转换系数存储为适合尺寸的基本单位。因此,如果您选择英尺作为距离的基本单位,请存储
要实际进行转换,一旦您检查尺寸是否匹配,只需乘以源单位的
系数
,然后除以目标单位的因子
。例如,要从米换算为英里,请乘以 3.28084,然后除以 5280。Pick a base unit for each dimension you are interested in (read that wiki page, it'll be useful). For example, if most of your data is in SI units, you would pick kilogram for mass, second for time, metre for distance, and so on. If most of your data is in US units, pick units from the US customary units, for example pound for mass, foot for length, second for time.
Then store, for each actual unit you want to be able to handle, the conversion factor to the dimensionally-appropriate base unit. So if you choose foot as your base unit of distance, store
To actually do a conversion, once you've checked that the dimensions match, simply multiply by the
Factor
of the source unit, and divide by theFactor
of the destination unit. For example, to get from metres to miles, multiply by 3.28084, then divide by 5280.ToUnit = (FromUnit + FromOffset) * 被乘数 / 分母 + ToOffset
ToUnit = (FromUnit + FromOffset) * Multiplicand / Denominator + ToOffset
我认为最初的帖子提出的模式很好,除了不包括 Class (如 Seth 的答案) - 你不想尝试在品脱和英寸之间进行转换。
如果两个单位都不是转换单位,则可以通过检索两个单位的转换记录并将一个因数除以另一个因数(例如 36/12 = 3 英尺一码)来简单地实现两个单位之间的转换。
如果您特别关心准确性,您可以确保给定类别的所有单元都具有同一类别中所有其他单元的条目 - 不过,这让我觉得有点矫枉过正。
I think the original post's proposed schema is fine, apart from not including Class (as in Seth's answer) - you don't want to try to convert between pints and inches.
Converting between two units where neither of them is the conversion unit is simply achieved by retrieving both units' conversion records and dividing one factor by the other (eg. 36/12 = 3 feet in a yard).
If you are particularly concerned about accuracy, you could ensure that all units for a given class have entries for all other units in the same class - this strikes me as overkill, though.
结束子
End Sub