Created
August 14, 2011 18:40
-
-
Save kijuky/1145165 to your computer and use it in GitHub Desktop.
UnitConversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g100pon #18 単位換算 | |
// JScience を使用します - http://jscience.org/ | |
// 注: 次期バージョン(5.0)では配布方法が変わるため、以下の Grab 指定が変わる可能性があります。 | |
// また、本バージョン(4.x)も、公式には Maven リポジトリとして配布されていません。 | |
// 現在は以下に示すサイトで暫定的に取得できます。 | |
//@GrabResolver(name = 'jcurl', root = 'http://www.jcurl.org/m2/repo/') | |
//@GrabResolver(name = 'obiba', root = 'http://maven.obiba.org/maven2/') | |
@Grab('org.jscience:jscience:4.3.1') | |
import javax.measure.DecimalMeasure | |
import static javax.measure.unit.SI.* | |
import static javax.measure.unit.NonSI.* | |
/* ミリやキロ */ | |
// 100メートルをキロメートルに | |
// 単位付き数字の指定は '数字' + ' ' + '単位' の形で指定。 | |
// 細かい指定方法は javax.measure.unit.UnitFormat を参照。 | |
println('100 メートルは ' + DecimalMeasure.valueOf('100 m').doubleValue(KILO(METER)) + ' キロメートル') | |
/* 異なる単位系へ */ | |
// 25℃を華氏度に | |
// > F = (9/5 * C) + 32 = (9/5 * 25) + 32 = 45 + 32 = 77 | |
println('セ氏 25 度はカ氏 ' + DecimalMeasure.valueOf('25 ℃').doubleValue(FAHRENHEIT) + ' 度') | |
/* 組立単位 */ | |
// 毎時10ミリシーベルトを毎秒レムに | |
// > (10 mSv/h) * (100 rem/Sv) * (1/3600 h/s) = (10 * 100 / 3600) * (mrem/s) = (0.277 / 1000) * (rem/s) = 2.7E-4 rem/s | |
println('毎時 10 ミリシーベルトは毎秒 ' + DecimalMeasure.valueOf('10 mSv/h').doubleValue(REM.divide(SECOND)) + ' レム') | |
/* JScienceには無い単位 */ | |
// 2500キロカロリーをジュールに | |
// > (2500 kcal) * (4.2 J/cal) = (2500 k) * (4.2 J) = (2500 * 4.2 kJ) = 10500 kJ = 10500E3 J = 1.05E7 J | |
import javax.measure.unit.UnitFormat | |
UnitFormat.getInstance().label(KILO(JOULE.times(4.2)), "kcal") // 1 kcal = 4.2 kJ | |
println('2500 キロカロリーは ' + DecimalMeasure.valueOf('2500 kcal').doubleValue(JOULE) + ' ジュール') | |
/* 有り得ない変換 */ | |
// 12ギガエレクトロンボルトをキログラムに | |
try { | |
DecimalMeasure.valueOf('12 GeV').doubleValue(KILOGRAM) | |
} catch (Throwable e) { | |
println(e) // javax.measure.converter.ConversionException: m is not compatible with kg | |
} | |
// 先ほどの変換は相対性理論が成り立つ系では、変換可能。(E = mc^2 より) | |
import org.jscience.physics.model.RelativisticModel | |
import javolution.context.LocalContext | |
LocalContext.enter() | |
try { | |
RelativisticModel.select() // この try 節のみ、相対性理論が成り立つ | |
println('12 ギガエレクトロンボルトは ' + DecimalMeasure.valueOf('12 GeV').doubleValue(KILOGRAM) + ' キログラム') | |
} finally { | |
LocalContext.exit() | |
} | |
// 為替 | |
// 円ドル変換(1ドル77円とする) | |
import org.jscience.economics.money.Currency // java.util.Currency とは互換性がないです。 | |
import static org.jscience.economics.money.Currency.* | |
UnitFormat.getInstance().label(JPY, '¥') // \u00A5 | |
Currency.setReferenceCurrency(USD) // ベースとなる貨幣単位の設定(ここでは米国ドル) | |
JPY.setExchangeRate(1 / 77) // レートの設定 (1 $ = 77 ¥) | |
println('2500 円は ' + DecimalMeasure.valueOf('2500 ¥').doubleValue(USD) + ' $') | |
// 独自通貨 | |
def MSP = new Currency('MSP') // ゲイツポイント - http://ja.wikipedia.org/wiki/%E3%83%9E%E3%82%A4%E3%82%AF%E3%83%AD%E3%82%BD%E3%83%95%E3%83%88%E3%83%9D%E3%82%A4%E3%83%B3%E3%83%88 | |
Currency.setReferenceCurrency(JPY) | |
MSP.setExchangeRate(1.5 / 1) // 1.5 ¥ = 1 MSP | |
println('2500 円は ' + DecimalMeasure.valueOf('2500 ¥').doubleValue(MSP) + ' MSP') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment