【问题标题】:measurement converter for selecting unit选择单位的测量转换器
【发布时间】:2012-02-06 03:50:32
【问题描述】:

我看到了很多与宝石相关的测量转换,但我无法找到能够选择最佳/最接近单位的宝石。

例如

如果我给宝石测量

9 英寸 + 6 英寸

我正在尝试得到结果

1英尺3英寸

我见过的转换工具,我必须告诉转换器尝试转换为英尺,然后决定哪个是最合适的度量。

【问题讨论】:

标签: ruby units-of-measurement


【解决方案1】:

不确定您想要变得多复杂,但对于您的示例,我这样做了:

def plain_english_conversion(inches)
    divmod_output = inches.divmod(12)
    puts "#{divmod_output[0]} ft, #{divmod_output[1]} in"
end

puts "15 "
plain_english_conversion(15)

puts "37 "
plain_english_conversion(37)

输出:

15

1 英尺 3 英寸

37

3 英尺,1 英寸

当然,我将其限制为英尺/英寸,但如有必要,您可以将其抽象出来(英寸和码、英尺和码等)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2019-02-03
    • 1970-01-01
    • 2020-08-01
    • 2013-04-09
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多