|
![]() |
![]()
temperature conversion |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
Frontier: adding narrative temperature conversion
Tuesday 26 July 2005
It's frustrating being in a metric-phobic country. Whenever we travel I find that metric temperatures just make sense. Readers from all over visit these pages, and to make life just a wee bit easier, I wrote a small script to provide temperatures in my narrative in both Farenheit and Celcius, and in the same precision. Here's some examples of the script in action:
Bowing to the logic that the contextually appropriate temperature scale should appear first, with the conversion following, I require only a value an a scale, so an American can write I have a fever; it's 98.6° F (37.0° C), whereas pretty much everyone else would say ... 37° C (98° F). The function looks like this {temp(98.6, "F")} and here's how it's implemented:
on temp( value, scale = "F" ) { « «Do the conversion mathematics « local { result = 0.0; oldScale, newScale, h, answer, precision}; if ( scale == "f" ) { scale = "F"}; if ( scale == "F") { result = (value - 32) * (5.0 / 9.0); oldScale = "F"; newScale = "C"} else { result = (value * (9.0 / 5.0)) + 32; oldScale = "C"; newScale = "F"}; « «Generate output that has at most the number of decimal places as the input. « dotPosition = string.PatternMatch( ".", value ); if ( dotPosition == 0 ) { precision = -1} else { precision = string.length(value) - dotPosition}; answer = string.mid( result, 0, string.patternMatch( ".", result ) + precision ); h = value + "° " + oldScale + " (" + answer + "° " + newScale + ")"; return( h )}; I hope you have as much fun with this as I do. It's nice to strike a blow for learning "that other system"...
|
This page is part of the Userland Frontier WebRing. | ![]() |
List all this webring's pages; visit another page; add your page to this webring. |
Have you found errors nontrivial or marginal, factual, analytical and illogical, arithmetical, temporal, or even typographical? Please let me know; drop me email. Thanks! |