36

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.)

Nate
  • 18,892
  • 27
  • 70
  • 93
  • 3
    You could take a look at this https://github.com/mbe24/sius by now it supports meter, mile for length and kilogram, pound for mass. It supports addition and does *implicit* type conversion. It is statically typed, so conversion errors are visibile at compile time :) I will add more features :) – mike Apr 27 '14 at 16:02

4 Answers4

58

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

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:

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.*;
dfa
  • 114,442
  • 31
  • 189
  • 228
  • 1
    Thanks for this; I had never heard of this but will be extremely useful in my work. – I82Much May 17 '10 at 19:51
  • 6
    Very nice, but the request was rejected and I downloaded JScience 5.0 and couldn't find an implementation like the one in the example shown here. Can anyone point to how this can be accomplished now? – CCC Jun 28 '11 at 03:02
  • JScience seems good, but i am not able to find proper documentation for that it has well documented API but i am looking for some thing like Wiki, if you have any links let me know :D – Yogesh Oct 03 '13 at 11:07
  • 6
    It's been relocated to github: https://github.com/unitsofmeasurement – assylias Oct 13 '16 at 15:45
  • 2
    Gradle dependency `implementation 'org.jscience:jscience:4.3.1'` – Vlad Feb 18 '19 at 19:46
  • 3
    On Android I needed to exclude `Javolution` as noted in this answer: https://stackoverflow.com/a/23841614/2625236 `compile ('org.jscience:jscience:4.3.1') { exclude group: 'org.javolution', module: 'javolution' }` – Vlad Mar 01 '19 at 09:48
  • https://mvnrepository.com/artifact/org.jscience/jscience/4.3.1 (continuation of Vlad's comment) – granadaCoder Aug 22 '19 at 12:35
4

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.

//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.

Digidemic
  • 71
  • 4
0

Adding to the pile...

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

mattexx
  • 6,456
  • 3
  • 36
  • 47
-2

I have found Java Unit Conversion library at 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.