简体   繁体   English

美国/英制单位和 Java 的测量单位 API

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

I'm only seeing metric units in the Indriya 2.0.4 reference implementation of the Java Units of Measurement standard.我只在 Java 测量单位标准的Indriya 2.0.4 参考实现中看到公制单位。 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<Mass> weight = Quantities.getQuantity(1, Units.KILOGRAM);我可以做Quantity<Mass> weight = Quantities.getQuantity(1, Units.KILOGRAM); , so where is Quantity<Mass> weight = Quantities.getQuantity(1, Units.POUND); ,所以Quantity<Mass> weight = Quantities.getQuantity(1, Units.POUND); ? ?

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).对于比 SI 单位更宽的测量,您可以使用Units of Measurement项目中的一些其他 API(其中包括indriya API)。

Specifically, the CLDR and USCustomary classes may be of interest.具体来说,可能对CLDRUSCustomary类感兴趣。 However, I have not had success combining units from different classes, eg 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<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");

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:我对此的 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>

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

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