US/Imperial units with Java’s Units of Measurement API

huangapple go评论76阅读模式
英文:

US/Imperial units with Java's Units of Measurement API

问题

我只在Java度量单位标准的Indriya 2.0.4参考实现中看到了公制单位。肯定有其他人想要使用英尺、英寸和英里,但我找不到相关信息。有人可以告诉我我漏掉了什么吗?

我可以这样做:Quantity<Mass> weight = Quantities.getQuantity(1, Units.KILOGRAM);,那么Quantity<Mass> weight = Quantities.getQuantity(1, Units.POUND);在哪里呢?

英文:

I'm only seeing metric units in the Indriya 2.0.4 reference implementation of the Java Units of Measurement standard. Surely other people have wanted to work with feet, inches, and miles, but I can't find anything about that. Can anyone tell me what I'm missing here?

I can do Quantity&lt;Mass&gt; weight = Quantities.getQuantity(1, Units.KILOGRAM);, so where is Quantity&lt;Mass&gt; weight = Quantities.getQuantity(1, Units.POUND);?

答案1

得分: 2

以下是翻译好的内容:

对于超出国际单位制的测量,您可以使用Units of Measurement项目中的其他一些API(包括indriya API)。

具体来说,CLDRUSCustomary类可能会引起兴趣。然而,我尝试将来自不同类别的单位组合在一起进行转换时没有成功(请参见下面的已注释掉的代码)。

一些示例:

import javax.measure.Quantity;
import javax.measure.quantity.Length;
import javax.measure.quantity.Mass;

import systems.uom.unicode.CLDR;
import systems.uom.common.USCustomary;

import tec.units.ri.quantity.Quantities;
import tec.units.ri.unit.Units;

...

Quantity<Mass> weight1 = Quantities.getQuantity(1, Units.KILOGRAM);
Quantity<Mass> weight2 = Quantities.getQuantity(1, USCustomary.POUND);

// javax.measure.IncommensurableException: kg is not compatible with lb
//Double d2 = Units.KILOGRAM.getConverterTo(CLDR.POUND).convert(1);

Quantity<Mass> weight3 = Quantities.getQuantity(123.45, CLDR.GRAM);
Quantity<Mass> weight4 = Quantities.getQuantity(123.45, CLDR.OUNCE);
Quantity<Mass> weight5 = Quantities.getQuantity(123.45, CLDR.OUNCE_TROY);

Double ounces = CLDR.GRAM.getConverterTo(CLDR.OUNCE)
        .convert(weight3.getValue()).doubleValue();
System.out.println(weight3.getValue() + " grams = " + ounces + " ounces");

Quantity<Length> dist1 = Quantities.getQuantity(123.45, CLDR.KILOMETER);
Double miles = CLDR.KILOMETER.getConverterTo(CLDR.MILE)
        .convert(dist1.getValue()).doubleValue();
System.out.println(dist1.getValue() + " kilometers = " + miles + " miles");

上面示例的输出为:

123.45 grams = 4.354570602675702 ounces
123.45 kilometers = 76.70827368169888 miles

我此处的POM依赖如下:

<dependencies>
    <dependency>
        <groupId>javax.measure</groupId>
        <artifactId>unit-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>tec.units</groupId>
        <artifactId>unit-ri</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>systems.uom</groupId>
        <artifactId>systems-unicode</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>systems.uom</groupId>
        <artifactId>systems-common</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>systems.uom</groupId>
        <artifactId>systems-quantity</artifactId>
        <version>2.0.2</version>
    </dependency>
</dependencies>
英文:

For measurements which are wider than just SI units, you can use some of the other APIs in the Units of Measurement project (which includes the indriya API).

Specifically, the CLDR and USCustomary classes may be of interest. However, I have not had success combining units from different classes, e.g. for conversions (see the commented-out code below).

Some examples:

import javax.measure.Quantity;
import javax.measure.quantity.Length;
import javax.measure.quantity.Mass;

import systems.uom.unicode.CLDR;
import systems.uom.common.USCustomary;

import tec.units.ri.quantity.Quantities;
import tec.units.ri.unit.Units;

...

Quantity&lt;Mass&gt; weight1 = Quantities.getQuantity(1, Units.KILOGRAM);
Quantity&lt;Mass&gt; weight2 = Quantities.getQuantity(1, USCustomary.POUND);
        
// javax.measure.IncommensurableException: kg is not compatible with lb
//Double d2 = Units.KILOGRAM.getConverterTo(CLDR.POUND).convert(1);

Quantity&lt;Mass&gt; weight3 = Quantities.getQuantity(123.45, CLDR.GRAM);
Quantity&lt;Mass&gt; weight4 = Quantities.getQuantity(123.45, CLDR.OUNCE);
Quantity&lt;Mass&gt; weight5 = Quantities.getQuantity(123.45, CLDR.OUNCE_TROY);
        
Double ounces = CLDR.GRAM.getConverterTo(CLDR.OUNCE)
        .convert(weight3.getValue()).doubleValue();
System.out.println(weight3.getValue() + &quot; grams = &quot; + ounces + &quot; ounces&quot;);
        
Quantity&lt;Length&gt; dist1 = Quantities.getQuantity(123.45, CLDR.KILOMETER);
Double miles = CLDR.KILOMETER.getConverterTo(CLDR.MILE)
        .convert(dist1.getValue()).doubleValue();
System.out.println(dist1.getValue() + &quot; kilometers = &quot; + miles + &quot; miles&quot;);

The outputs from the above examples are:

123.45 grams = 4.354570602675702 ounces
123.45 kilometers = 76.70827368169888 miles

My POM dependencies for this are:

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.measure&lt;/groupId&gt;
        &lt;artifactId&gt;unit-api&lt;/artifactId&gt;
        &lt;version&gt;2.0&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;tec.units&lt;/groupId&gt;
        &lt;artifactId&gt;unit-ri&lt;/artifactId&gt;
        &lt;version&gt;1.0.3&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;systems.uom&lt;/groupId&gt;
        &lt;artifactId&gt;systems-unicode&lt;/artifactId&gt;
        &lt;version&gt;2.0.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;systems.uom&lt;/groupId&gt;
        &lt;artifactId&gt;systems-common&lt;/artifactId&gt;
        &lt;version&gt;2.0.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;systems.uom&lt;/groupId&gt;
        &lt;artifactId&gt;systems-quantity&lt;/artifactId&gt;
        &lt;version&gt;2.0.2&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

huangapple
  • 本文由 发表于 2020年8月1日 00:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63195863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定