Added unit conversion for various lengths.

This commit is contained in:
reemo 2025-06-08 03:09:21 +00:00 committed by flashwave
parent 7fdca6ccc0
commit 74b6d71cb8
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
2 changed files with 117 additions and 2 deletions

111
conv/length.lua Normal file
View file

@ -0,0 +1,111 @@
local function cm2in(cm) return cm / 2.54 end
local function in2cm(inch) return inch * 2.54 end
local function m2ft(m) return cm2in(m * 100) / 12 end
local function ft2m(ft) return in2cm(ft * 12) / 100 end
local function km2mi(km) return m2ft(km * 1000) / 5280 end
local function mi2km(mi) return ft2m(mi * 5280) / 1000 end
local function pretty2ugly(pretty)
local feet, inches = string.match(pretty, "([%d%.]+)%'([%d%.]+)%\"")
if feet == nil then
feet = string.match(pretty, "([%d%.]+)%'")
inches = 0
end
if feet == nil then
feet = 0
inches = string.match(pretty, "([%d%.]+)%\"")
end
if inches == nil then return 0
else return (tonumber(feet) or 0) + ((tonumber(inches) or 0) / 12) end
end
local function ugly2pretty(ugly)
ugly = tonumber(ugly) or 0
local feet, inches = math.modf(ugly)
inches = inches * 12
return (feet > 0 and feet .. "'" or "")
.. (inches > 0 and string.format("%.4g", inches) .. "\"" or "")
end
local function convertLength(args, func, str, prettyin, prettyout)
prettyin = prettyin or false
prettyout = prettyout or false
if #args.args < 2 then
args.send("[i]No length argument specified.[/i]")
return
end
local length = args.args[2]
if prettyin and tonumber(length) == nil then
length = pretty2ugly(length)
end
if tonumber(length) == nil then
args.send("[i]Length argument is invalid.[/i]")
return
end
out = func(length)
if prettyin then
length = ugly2pretty(length)
end
if prettyout then
out = ugly2pretty(out)
end
args.send(string.format(str, length, out))
end
kmj.commands.register({
name = "in2cm",
summary = "Converts inches to centimeters.",
dispatch = function(args)
convertLength(args, in2cm, '[b]%g"[/b] = [b]%g cm[/b]')
end
})
kmj.commands.register({
name = "cm2in",
summary = "Converts centimeters to inches.",
dispatch = function(args)
convertLength(args, cm2in, '[b]%g cm[/b] = [b]%g"[/b]')
end
})
kmj.commands.register({
name = "ft2m",
summary = "Converts feet-inches to meters.",
dispatch = function(args)
convertLength(args, ft2m, '[b]%s[/b] = [b]%g m[/b]', true, false)
end
})
kmj.commands.register({
name = "m2ft",
summary = "Converts meters to feet-inches.",
dispatch = function(args)
convertLength(args, m2ft, '[b]%g m[/b] = [b]%s[/b]', false, true)
end
})
kmj.commands.register({
name = "mi2km",
summary = "Converts miles to kilometers.",
dispatch = function(args)
convertLength(args, mi2km, '[b]%g mi[/b] = [b]%g km[/b]')
end
})
kmj.commands.register({
name = "km2mi",
summary = "Converts kilometers to miles.",
dispatch = function(args)
convertLength(args, km2mi, '[b]%g km[/b] = [b]%g mi[/b]')
end
})

View file

@ -1,6 +1,10 @@
version = "1.0.0"
version = "1.1.0"
description = "Converts between formats."
authors = ["flashwave <me@flash.moe>"]
authors = [
"flashwave <me@flash.moe>",
"reemo"
]
sources = [
"length.lua",
"temp.lua"
]