Java 度量单位转换库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1193810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:44:31  来源:igfitidea点击:

Java Metric Unit Conversion Library?

java

提问by Nate

I have an application that will need to perform a number of unit conversions (metric to Imperial, Imperial to metric).

我有一个应用程序需要执行许多单位转换(公制到英制,英制到公制)。

Is there an existing Java library that does this? Or will I need to roll my own? (My initial Google searches proved moderately useless.)

是否有一个现有的 Java 库可以做到这一点?还是我需要自己动手?(事实证明,我最初的 Google 搜索毫无用处。)

回答by dfa

there is a specific JSR 275(javax.measure) with JScienceas RI (Reference Implementation). For example converting 100 Miles to kilometers is easy as:

有一个特定的JSR 275(javax.measure),其中JScience作为 RI(参考实现)。例如,将 100 英里转换为公里很容易,如下所示:

UnitConverter toKilometers = MILE.getConverterTo(KILOMETER);
double km = toKilometers.convert(Measure.valueOf(100, MILE).doubleValue(MILE));

(note that units are all type safe a compile-time, a killer feature imho)

(请注意,单位在编译时都是类型安全的,恕我直言,这是一个杀手级功能)

The reverse can be easy:

反过来很容易:

UnitConverter toMiles1 = KILOMETER.getConverterTo(MILE);

or supereasy as:

或超级简单:

UnitConverter toMiles2 = toKilometers.inverse();

NB imports:

NB进口:

import javax.measure.Measure;
import javax.measure.converter.UnitConverter;
import javax.measure.quantity.Length;
import static javax.measure.unit.NonSI.*;
import static javax.measure.unit.SI.*;

回答by Martin Vondrá?ek

I have found Java Unit Conversion libraryat SourceForge, but I have not yet tried it (I will need similar funcionality in near future). It is licensed as BSD license. It might help.

我在 SourceForge找到了Java 单位转换库,但我还没有尝试过(我将在不久的将来需要类似的功能)。它被许可为 BSD 许可证。它可能会有所帮助。

回答by mattexx

Adding to the pile...

添加到堆...

Unidata now publishes the java units conversion library from netcdf-javaas a standalone package.

Unidata 现在将来自netcdf-java的 java 单位转换库作为独立包发布

回答by Digidemic

We created a library, UnitOf, for Java, JavaScript, and C# to easily perform these measurement conversions. There are are over 20 complete units of measure and conversions can be done in as little as one line.

我们为 Java、JavaScript 和 C#创建了一个库UnitOf来轻松执行这些测量转换。有超过 20 种完整的度量单位,只需一行即可完成转换。

//One liner
double x = new UnitOf.Mass().fromPounds(5).toKilograms(); //2.26796 returned as 5 pounds is 2.26796 kilograms

//Or as a variable
UnitOf.Length feet = new UnitOf.Length().fromFeet(5.5); //Instantiate UnitOf.Length and set "feet" as 5.5
double foo = feet.toInches(); //66 returned as 5.5 feet is 66 inches
double bar = feet.toMeters(); //1.6764 returned as 5.5 feet is 1.6764 meters

UnitOf also gives the ability to parse data types, convert to and from fractions, and even create your own custom UnitOf measurements.

UnitOf 还提供了解析数据类型、与分数相互转换、甚至创建您自己的自定义 UnitOf 度量的能力。