Vijay Vennapusa
Vijay Vennapusa

Reputation: 179

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

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 = ?

Upvotes: 1

Views: 459

Answers (2)

duffymo
duffymo

Reputation: 308998

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.

Upvotes: 0

Mike Vella
Mike Vella

Reputation: 10575

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.

Upvotes: 3

Related Questions