简体   繁体   中英

is there any library in java for unit conversion?

I want to use unit conversions like kg to grams, lit to ml, etc.. is there any java library and my-sql tables, available for this ?

I was asked to use JScience so now I have to populate all the units in JScience into a JCombo box or a list box.. Tel me hod do i do that please ?

You can use JScience ...

Here's an example to convert kilogram to gram:-

    double gram = Measure.valueOf(5, SI.KILOGRAM).doubleValue(SI.GRAM);
    System.out.println(gram);

Here are all the available units:-

    for (Unit<?> unit : SI.units()) {
        System.out.println(unit);
    }

The print outs:

m/s?
F
lm
A
C
N
H
J
lx
K
m/s
Wb
m?
Gy
T
W
kg
V
Ω
S
kat
Pa
sr
m
m?
mol
bit
°C
Hz
s
Bq
Sv
rad
cd

By the way, I'm using the JRE 1.4 compatible binary from JScience , and I also need JSR 275 (I pulled that from Maven):

    <dependency>
        <groupId>javax.measure</groupId>
        <artifactId>jsr-275</artifactId>
        <version>1.0.0</version>
    </dependency>

Here're my import statements, if anyone cares:-

import javax.measure.units.SI;
import javax.measure.units.Unit;
import org.jscience.physics.measures.Measure;

JUCL . Also, see related questions (they recommend JScience there).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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