9
9
language =" Lua"
10
10
purpose =" Provides a simple calculator"
11
11
date_written =" 2010-08-25 14:55:13"
12
- date_modified =" 2014-09-27 10:09 :00"
12
+ date_modified =" 2015-01-10 14:11 :00"
13
13
requires =" 4.50"
14
- version =" 1.1 "
14
+ version =" 1.2 "
15
15
>
16
16
<description trim =" y" >
17
17
<![CDATA[
38
38
= hex (1234)
39
39
= oct (5678)
40
40
= bin (15)
41
+
42
+ If the number is small (with an exponent shown) it is echoed again with up to 15 decimal places.
43
+
44
+ eg. 28 * 16e-9 = 4.48e-007 (0.000000448)
45
+
41
46
]]>
42
47
</description >
43
48
@@ -59,8 +64,22 @@ bit.hex = function (num) return bit.tostring (num, 16) end
59
64
bit.oct = function (num) return bit.tostring (num, 8) end
60
65
bit.bin = function (num) return bit.tostring (num, 2) end
61
66
62
- print ("%1 =", setfenv (assert (loadstring "return %1"), setmetatable ({},
63
- {__index = function (_, n) return math [n] or bit [n] or string [n] or utils [n] or world [n] end}) ) () )
67
+ result = setfenv (assert (loadstring "return %1"), setmetatable ({},
68
+ {__index = function (_, n) return math [n] or bit [n] or string [n] or utils [n] or world [n] end}) ) ()
69
+
70
+ -- turn result into a string, to see if it has "e-xxx" in it.
71
+ formatted_result = tostring (result)
72
+
73
+ -- if an exponent shown, recalculate as a decimal without exponent
74
+ if string.match (formatted_result, "e%%-") then
75
+ local extra = string.format (" (%%0.15f)", result)
76
+
77
+ -- append number without exponent, omitting trailing zeroes
78
+ formatted_result = formatted_result .. string.gsub (extra, "0+%%)", ")")
79
+ end -- if
80
+
81
+ -- display result
82
+ print ("%1 =", formatted_result)
64
83
</send >
65
84
</alias >
66
85
</aliases >
0 commit comments