Friday, December 10, 2010 at 12:01 AM.
system.verbs.builtins.string.formatDouble
on formatDouble (num, placesRightOfDecimal=3) {
<<Changes
<<12/9/10; 12:34:17 PM by DW
<<Changed the constant "00" to a string.filledstring. placesRightOfDecimal wasn't always a parameter, it was hardcoded to 3. Now the 0-padding has to allow for values other than 3.
<<12/7/10; 5:47:04 PM by DW
<<Send us a double and we'll send back a nicely formatted number without a huge number of places to the right of the decimal point. This code was being cribbed, it should be factored.
local (s = string (num));
if s contains "." {
local (lod = string.nthfield (s, ".", 1));
local (rod = string.nthfield (s, ".", 2) + string.filledstring ("0", placesRightOfDecimal - 1));
if sizeof (rod) > placesRightOfDecimal {
rod = string.mid (rod, 1, placesRightOfDecimal)};
return (lod + "." + rod)}
else {
return (s)}}
<<bundle //test code
<<dialog.alert (formatDouble (double (random (1, clock.ticks ())) / 1000, 7))
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.