Pages

Monday, November 20, 2017

Just some conversion tool...

Aloha,

Last week I was in Switzerland and somehow got reminded on some tool that I've created some time ago which might be handy for some of you. It is a simple unit conversion tool which supports conversion between the following categories of units:
- ACCELERATION 
- ANGLE 
- AREA 
- DATA 
- CURRENT 
- ELECTRIC_CHARGE 
- ENERGY 
- FORCE 
- HUMIDITY 
- LENGTH 
- LUMINANCE 
- LUMINOUS_FLUX 
- MASS 
- PRESSURE 
- SPEED 
- TEMPERATURE 
- TEMPERATURE_GRADIENT 
- TIME 
- TORQUE 
- VOLUME 
- VOLTAGE 
- WORK
As an example let's convert a temperature in Celsius in Fahrenheit and Kelvin which would 
look as follows: 
Converter temperatureConverter = new Converter(TEMPERATURE, CELSIUS);

double celsius    = 32.0;
double fahrenheit = temperatureConverter.convert(celsius, FAHRENHEIT);
double kelvin     = temperatureConverter.convert(celsius, KELVIN);

System.out.println(celsius + "°C => " + fahrenheit + "°F => " + kelvin + "°K");

So first you create a Converter instance with a category and a base unit (here Temperature 
as category and Celsius as base unit).
After that is done you can convert celsius based temperatures to other units like 
Fahrenheit and Kelvin.
In addition I've also added a method to shorten long numbers with abbreviations. 
Sometimes this is really useful when working with big numbers, 
a little example would look like follows...

System.out.println(Converter.format(1_500_000, 1));

System.out.println(Converter.format(1_000_000, 0)); 
And the result will look like this... 

1.5M 

1M

The format method will support the following abbreviations
- kilo 
- Mega 
- Giga 
- Tera 
- Peta 
- Exa 
- Zetta 
- Yotta

Not really sure if you can use it but at least I wanted to share it with you folks... :) The code is as always available on github. That's it for today...keep coding...

No comments:

Post a Comment