# Visual unit conversion

To make my game appealing to a wider range audience, expecially those who use the imperial system, I’ve decided to make this moduleScript for my game to automatically convert distance, temperature and mass to the user’s preffered unit. This also allows for scaling, where the largest unit is shown.

Here’s the module I cooked up:

``````local function rnd(n, d)
return math.round(n/d) * d
end

local temp =
{
C = function(c) return rnd(c, .1).."°C" end,
F = function(c) return rnd((c*9/5) + 32, .1).."°F" end,
K = function(c) return rnd(c + 273.15, 0.1).."°K" end
}

local mass =
{
KG = function(kilos)
local log = math.log10(kilos)
if log >= 3 then
return rnd(kilos / 1e3, .1).."t"
elseif log >= -0.005 then
return rnd(kilos, .1).."kg"
elseif log >= -3 then
return rnd(kilos * 1e3, .1).."g"
elseif log >= -6 then
return rnd(kilos * 1e6, .1).."mg"
elseif log >= -9 then
return rnd(kilos * 1e9, .1).."µg"
elseif log >= -12 then
return rnd(kilos * 1e12, .1).."ng"
elseif log>= -15 then
return rnd(kilos * 1e15, .1).."pg"
else
return rnd(kilos * 1e18, .1).."fg"
end
end,
LB = function(kilos)
local tonnes = kilos / 1016.047
local pounds = kilos * 2.205
local stone = pounds / 14
local ounces = pounds * 16
if tonnes >= 1 then
return rnd(tonnes, .1).."t"
end
if stone > 8 then
return rnd(stone, 1).."st"
elseif stone > 3 then
return rnd(stone, 1).."st, "..rnd(pounds % 16, 1).."lb"
elseif pounds < 3 then
return rnd(ounces, 0.1).."oz"
else
return rnd(pounds, 1).."lb, "..rnd(pounds // 16, .1).."oz"
end
end,
}

local dist =
{
KM = function(meters)
if meters > 1000 then
return rnd(meters / 1000, .1).."km"
elseif meters < 1 and meters > 0.1 then
return rnd(meters * 100, 1).."cm"
else
return rnd(meters * 1000, .1).."mm"
end
end,
MI = function(meters)
local feet = meters * 3.28084
local miles = feet // 5280
local inches = feet * 12
local yards = feet / 3

if miles > 1 then
return rnd(miles, .1).."mi"
elseif feet > 10 then
return rnd(feet, 1).."ft"
elseif feet > 2 then
return rnd(feet, 1).."ft, "..rnd(inches, 1)..'in'
else
return rnd(inches, .1)..'in'
end
end,
}

local convFuncs = {}

local userConfig = game.ReplicatedStorage.local_units

convFuncs.temperature = function(celcius): string
return temp[userConfig.temp_unit.Value](celcius)
end

convFuncs.distance = function(meters): string
return dist[userConfig.dist_unit.Value](meters)
end

convFuncs.mass = function(kilograms): string
return mass[userConfig.mass_unit.Value](kilograms)
end

return convFuncs
``````

context
In replicated storage, the config “local_units” holds the settings.

These settings are client specific and are only changed by the client. New players would automatically have their unit prefferances selected based on their region, and returning players will have their unit prefferances unloaded from a datastore.

So… what is the question ? Do you need help or not ?

Category called “Creations Feedback”, I want feedback on my unit convertions, and if my implementation of them are visually appealing and accurate.

Patched a bug in metric mass convertion, now using log10 to determine at what scale to present at.

Also two lower scales of magnitude for metric masses:

Also I have no idea how to use imperial masses below the ounce, so if anyone knows, please inform me!