Monday, November 08, 2010 at 12:06 AM.
system.verbs.builtins.string.ratio
on ratio (numerator, denominator, places) { //return a string that represents a fraction
on powerOf (num, power) {
local (val = num, i, prevval);
for i = 1 to (power - 1) {
prevval = val;
val = val * num;
if (val / num) != prevval {
scriptError ("Can't raise " + num + " to the power of " + power + " because of integer overflow.")}};
return (val)};
local (num = (double (numerator) / denominator));
local (shifter = powerOf (10, places));
num = long (num * shifter); //let the rounding code kick in
num = double (num) / shifter;
return (string (num))}
<<bundle //test code
<<dialog.alert (ratio (1, 3, 2))
This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.