How to write units in Java like Radian, Newton, Pascal, mm, kg?

443 views Asked by At

I am developing a Java application, its about converting Mathcad worksheet to Java application. Can anyone know/suggest how to write units(newton, pascal, mm, kg)in java code?

Example:1?

double var1 = 10*newton/mm;//looking for a way to write the code in this format.
double var2 = 5*mmpower2;
double var = var1/var2;
double var = ?

Example: 2?

double var1 = 90degress-tan(60)*Radian;
double var1 = ?
2

There are 2 answers

0
Mike Vella On BEST ANSWER

You could use the Javax.units package, especially the Java Unit Class.

This is a well-studied problem and for any non-trivial program you should always use an existing units library.

1
duffymo On

Neither of your examples are sensible Java.

You want to implement the Martin Fowler Analysis Patterns idea of Quantity with units.

There are libraries that exist, like JScience, but I don't know if they're in widespread use.