Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- scriptName: dec2Eng
- -- by: dkh503@gmail.com
- -- Enter a number and get the Engineering Notation.
- function dec2Eng(x)
- assert(tonumber(x), "Error: invalid entry.")
- y = 0
- while x >= 1 do
- x = x / 10
- y = y + 1
- end
- while x < 1 do
- x = x * 10
- y = y - 1
- end
- if (y % 3) == 1 then
- y = y - 1
- x = x * 10
- elseif (y % 3) == 2 then
- y = y + 1
- x = x / 10
- end
- if x < 1 then
- x = x * 1000
- y = y - 3
- end
- return string.format("%.3fe%d", x, y)
- end
- io.write(
- [[----------------------------------
- ** dec2Eng.lua **
- Converts decimal, hex or
- scientific notation entries to
- Engineering Notation
- ----------------------------------]])
- io.write("\nEnter a number : ")
- z = dec2Eng(io.read("*n"))
- io.write(string.format("Engineering Notation: %s\n\n", z))
- -- Engineering notation:
- -- 1. Uses exponents in multiples of 3 only.
- -- 2. 0 < Base <1000
- -- This matches up with "Metric Prefixes" like milli[amps],
- -- micro[meters], and nano[farads] (negative exponents);
- -- and kilo[ohms], mega[hertz], and giga[bytes] (positive exponents),
- -- often seen in electronic and engineering measurements.
- --[[https://en.wikipedia.org/wiki/Metric_prefix
- Tera T 1000000000000 10e12
- Giga G 1000000000 10e9
- Mega M 1000000 10e6
- Kilo K 1000 10e3
- (none) (none) 1 10e0
- milli m 0.001 10e-3
- micro ยต 0.000001 10e-6
- nano n 0.000000001 10e-9
- pico p 0.000000000001 10e-12
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement