Skip to content

Commit cba48ce

Browse files
committed
Improvements to calculator plugin, show small number without exponent
1 parent 7d44a4f commit cba48ce

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

plugins/Calculator.xml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
language="Lua"
1010
purpose="Provides a simple calculator"
1111
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"
1313
requires="4.50"
14-
version="1.1"
14+
version="1.2"
1515
>
1616
<description trim="y">
1717
<![CDATA[
@@ -38,6 +38,11 @@ eg.
3838
= hex (1234)
3939
= oct (5678)
4040
= 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+
4146
]]>
4247
</description>
4348

@@ -59,8 +64,22 @@ bit.hex = function (num) return bit.tostring (num, 16) end
5964
bit.oct = function (num) return bit.tostring (num, 8) end
6065
bit.bin = function (num) return bit.tostring (num, 2) end
6166

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)
6483
</send>
6584
</alias>
6685
</aliases>

0 commit comments

Comments
 (0)