https://github.com/netomi/uom
A library to represent quantities and perform conversions between units of measurements with double or arbitray precision.
https://github.com/netomi/uom
java units-converter units-of-measurement unitsofmeasurement
Last synced: 10 months ago
JSON representation
A library to represent quantities and perform conversions between units of measurements with double or arbitray precision.
- Host: GitHub
- URL: https://github.com/netomi/uom
- Owner: netomi
- License: apache-2.0
- Created: 2020-03-09T07:20:29.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-17T11:39:01.000Z (over 3 years ago)
- Last Synced: 2025-04-01T04:04:12.524Z (12 months ago)
- Topics: java, units-converter, units-of-measurement, unitsofmeasurement
- Language: Java
- Homepage:
- Size: 13.3 MB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Units of Measurement
[](https://travis-ci.org/netomi/uom)
[](https://coveralls.io/github/netomi/uom?branch=master)
[](https://oss.sonatype.org/content/repositories/snapshots/com/github/netomi/uom/)
[](http://www.apache.org/licenses/LICENSE-2.0.html)
My personal take on a _units of measurement_ library for java.
The design goal of the library is to include:
* fully typed quantities:
```java
Length l1 = Length.of(1, SI.METRE);
Length l2 = Length.ofMeter(2); // convenience factory method for SI unit
```
* transparent support for double and arbitrary decimal precision quantities (using BigDecimal):
```java
Length l1 = Quantities.create(1, SI.METRE);
Length l2 = Quantities.create(BigDecimal.ONE, Intl.YARD);
Length l3 = l1.add(l2); // quantities with different precision can be used together
```
* support for generic quantities:
```java
Quantity speed = Quantities.createGeneric(1, SI.METER_PER_SECOND);
System.out.println(speed.add(Speed.ofMeterPerSecond(2))); // -> prints 3 m/s
```
* support for quantity factories:
```java
QuantityFactory factory = DoubleQuantity.factory(Length.class);
Length l1 = factory.create(1, SI.METRE);
// default factories can be replaced
// use decimal precision with MathContext DECIMAL128 for every quantity of type Length:
Quantities.registerQuantityFactory(Length.class, DecimalQuantity.factory(MathContext.DECIMAL128, Length.class));
// quantity factories with pooling behavior can be registered
QuantityFactory myPoolFactory = ...;
Quantities.registerQuantityFactory(Length.class, myPoolFactory);
```
* support for the standard unit manipulations as defined by [JSR-385](https://www.jcp.org/en/jsr/detail?id=385) et al
* support for as many units as possible, the amazing [GNU units](https://www.gnu.org/software/units/) library is the reference to compare to
The library uses dynamic proxies to create concrete quantity classes for the specified
precision at runtime.
## Examples
Conversion of units between different system of units, e.g. SI and CGS
```java
ElectricCharge e1 = ElectricCharge.of(1, SI.COULOMB);
ElectricCharge e2 = e1.to(ESU.STATCOULOMB);
System.out.println(e2);
```
prints
```2.997925e+09 statC```
Custom quantities and units:
```java
public interface Bmi extends Quantity {}
...
final Unit bmiUnit = SI.KILOGRAM.divide(SI.METRE.pow(2)).withSymbol("B").forQuantity(Bmi.class);
Quantity bmiDouble = Quantities.createGeneric(19, bmiUnit);
Quantity bmiDecimal = Quantities.createGeneric(BigDecimal.valueOf(21), bmiUnit);
System.out.println(bmiDouble);
```
License
-------
Code is under the [Apache Licence v2](https://www.apache.org/licenses/LICENSE-2.0.txt).