1

I have a database table which stores the units name in full like liters, kilograms, milliliters, milligrams etc.. I need a library to recogonize these units and convert it to the unit I wish to. How do i do this ?

Code Logic:

I will read the unit "liters" from database and i wish to convert it to milli-liters so her the input is "20 liters" and output should be "20000 milli-liters"

I downloaded JScience library but i am not sure how to do this. please tel me how to use that or suggest any alternative. It would be better if you explain me with a code sample. Thanks!!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Deepak
  • 6,684
  • 18
  • 69
  • 121

2 Answers2

2

I'm inclined to say use Frink, but it does WAY more than you need here, although it does solve the problem

20 litres -> milliliters
//gives 20000

And it is just a cool little language. In java you'd have to run it like any other scripting library. The website has loads of info

Java Drinker
  • 3,127
  • 1
  • 21
  • 19
  • is it a library that i can plug in to my existing application and use it ? – Deepak Apr 20 '11 at 13:55
  • 1
    It is really a full on programming language, based around the idea of calculations and unit conversions. But you **can** use it as a library in your Java application. See the documentation here: http://futureboy.us/frinkdocs/integrate/ – Java Drinker Apr 20 '11 at 14:04
  • thanks for that advice mate.. i will have a look at it also. Actually i started writing my own phraser for JScience. but Frink is really a good stuff to use... ll give it a try if i dont succeed in using JScience. Thank you very much !!! – Deepak Apr 20 '11 at 14:07
  • 1
    You're welcome. It may be of good use if you find you have lots of different types of units (litres, meters, pounds, cubic feet etc). The jar file is about 1Mb, so if that's not a big deal, i would add it even if only for basic unit conversions – Java Drinker Apr 20 '11 at 14:15
1

I'm not aware that JScience provides a facility for parsing strings like "20 liters" (that spelling makes me cringe...), so you'll probably have to handle that yourself, by tokenizing the string into quantities and units.

Once you have that, you can use JScience to convert between units easily enough, although obviously converting from litres to milliltres is trivial. But in principle, it's something like:

Measure<Integer, Volume> input = Measure.valueOf(20, NonSI.LITRE);
Measure<Integer, Volume> output = input.to(SI.MILLI(NonSI.LITRE));

System.out.println(output);
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • i can separate the units from the number. but can we make the JScience understand "litres" means the standard NonSI.LITRE. The problem is i have lot of units like that so its hard for me to write hal=ndling code for each and every unit i am using.so how do we do tat ? – Deepak Apr 20 '11 at 13:39
  • @Deepak: No, you'll have to parse the units yourself. There aren't *that* many of them. – skaffman Apr 20 '11 at 13:43
  • where will the MilliLiters grouped under ? i cant find it inside SI and in NonSI... – Deepak Apr 20 '11 at 13:56
  • can i use it like this SI.MILLI(NonSI.LITER); ? – Deepak Apr 20 '11 at 13:57
  • @Deepak: Yes, that's what my answer says – skaffman Apr 20 '11 at 14:09