Java单位转换
我有一个数据库表,其中完整存储单位名称,如升、千克、毫升、毫克等。我需要一个库来识别这些单位并将其转换为我想要的单位。我该怎么做?
代码逻辑:
我将从数据库中读取单位“升”,我希望将其转换为毫升,因此她的输入是“20升”,输出应该是“20000毫升”
我下载了JScience库,但我没有确定如何做到这一点。请告诉我如何使用它或建议任何替代方案。如果你能用代码示例来解释一下就更好了。谢谢!!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于说使用 Frink,但它的作用远远超出了您的需要,尽管它确实解决了问题
它只是一门很酷的小语言。在 Java 中,您必须像任何其他脚本库一样运行它。该网站有大量信息
I'm inclined to say use Frink, but it does WAY more than you need here, although it does solve the problem
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
我不知道 JScience 提供了解析像“20 litre”这样的字符串的工具(这个拼写让我感到畏缩......),所以你可能必须自己处理这个问题,将字符串标记为数量和单位。
一旦掌握了这一点,您就可以使用 JScience 轻松地在单位之间进行转换,尽管显然从升转换为毫升是微不足道的。但原则上,它是这样的:
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: