如何为单位转换表建模?

发布于 2024-09-27 16:47:52 字数 375 浏览 6 评论 0原文

我正在寻找创建各种单元及其相互关系的数据库模型。例如,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 技术交流群。

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

发布评论

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

评论(5

打小就很酷 2024-10-04 16:47:52

存储转换为 SI 单位,而不是其他非公制单位。然后您可以在单位之间进行转换,而无需知道显式转换。

Unit         | Class      | Base Unit Multiplier
------------------------------------------------------
foot         | length     | 0.304800610
yard         | length     | 0.914401830
square foot  | area       | 0.092903040
...

所以 14 英尺换算为:

14 feet * 0.304800610 = 4.26720854 meters
4.26720854 meters * 0.914401830⁻¹ = 4.66666667 yards

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.

Unit         | Class      | Base Unit Multiplier
------------------------------------------------------
foot         | length     | 0.304800610
yard         | length     | 0.914401830
square foot  | area       | 0.092903040
...

So 14 feet in yards is:

14 feet * 0.304800610 = 4.26720854 meters
4.26720854 meters * 0.914401830⁻¹ = 4.66666667 yards
谜兔 2024-10-04 16:47:52

为您感兴趣的每个维度选择一个基本单位(阅读该维基页面,它将是有用)。例如,如果大部分数据采用 SI 单位,则您可以选择千克表示质量、秒表示时间、米表示距离等。如果您的大部分数据采用美国单位,请从美国习惯单位中选择单位,例如英镑质量,英尺,长度,秒,时间。

然后,对于您希望能够处理的每个实际单位,将转换系数存储为适合尺寸的基本单位。因此,如果您选择英尺作为距离的基本单位,请存储

Unit    Dimension    Factor
Foot    Distance     1
Metre   Distance     3.28084
Mile    Distance     5280

要实际进行转换,一旦您检查尺寸是否匹配,只需乘以源单位的系数,然后除以目标单位的因子。例如,要从米换算为英里,请乘以 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

Unit    Dimension    Factor
Foot    Distance     1
Metre   Distance     3.28084
Mile    Distance     5280

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 the Factor of the destination unit. For example, to get from metres to miles, multiply by 3.28084, then divide by 5280.

迷你仙 2024-10-04 16:47:52
CREATE TABLE UnitConversion
(
    [FromUnit] NVARCHAR(100),
    [ToUnit] NVARCHAR(100),
    [FromOffset] DECIMAL(29,10),
    [Multiplicand] DECIMAL(29,10),
    [Denominator] DECIMAL(29,10),
    [ToOffset] DECIMAL(29,10)
)

ToUnit = (FromUnit + FromOffset) * 被乘数 / 分母 + ToOffset

CREATE TABLE UnitConversion
(
    [FromUnit] NVARCHAR(100),
    [ToUnit] NVARCHAR(100),
    [FromOffset] DECIMAL(29,10),
    [Multiplicand] DECIMAL(29,10),
    [Denominator] DECIMAL(29,10),
    [ToOffset] DECIMAL(29,10)
)

ToUnit = (FromUnit + FromOffset) * Multiplicand / Denominator + ToOffset

甜`诱少女 2024-10-04 16:47:52

我认为最初的帖子提出的模式很好,除了不包括 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.

雪花飘飘的天空 2024-10-04 16:47:52
Private Sub ConvertUnits()
Dim input As Double = Double.Parse(txtInput.Text)
Dim result As Double = 0

Select Case cmbUnit1.SelectedItem.ToString()
    Case "Mg"
        Select Case cmbUnit2.SelectedItem.ToString()
            Case "Mg"
                result = input
            Case "g"
                result = input * 1000
            Case "Kg"
                result = input * 1000000
            Case "Tonne"
                result = input * 1000000000
            Case "Grain"
                result = input * 0.0154324
            Case "Ounce"
                result = input * 28349.5
            Case "Pound"
                result = input * 453592
            Case "Stone"
                result = input * 6350293.18
        End Select
    Case "g"
        ' Code for converting from g to other units
    Case "Kg"
        ' Code for converting from Kg to other units
    Case "Tonne"
        ' Code for converting from Tonne to other units
    Case "Grain"
        ' Code for converting from Grain to other units
    Case "Ounce"
        ' Code for converting from Ounce to other units
    Case "Pound"
        ' Code for converting from Pound to other units
    Case "Stone"
        ' Code for converting from Stone to other units
End Select

lblResult.Text = result.ToString()

结束子

Private Sub ConvertUnits()
Dim input As Double = Double.Parse(txtInput.Text)
Dim result As Double = 0

Select Case cmbUnit1.SelectedItem.ToString()
    Case "Mg"
        Select Case cmbUnit2.SelectedItem.ToString()
            Case "Mg"
                result = input
            Case "g"
                result = input * 1000
            Case "Kg"
                result = input * 1000000
            Case "Tonne"
                result = input * 1000000000
            Case "Grain"
                result = input * 0.0154324
            Case "Ounce"
                result = input * 28349.5
            Case "Pound"
                result = input * 453592
            Case "Stone"
                result = input * 6350293.18
        End Select
    Case "g"
        ' Code for converting from g to other units
    Case "Kg"
        ' Code for converting from Kg to other units
    Case "Tonne"
        ' Code for converting from Tonne to other units
    Case "Grain"
        ' Code for converting from Grain to other units
    Case "Ounce"
        ' Code for converting from Ounce to other units
    Case "Pound"
        ' Code for converting from Pound to other units
    Case "Stone"
        ' Code for converting from Stone to other units
End Select

lblResult.Text = result.ToString()

End Sub

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