Modul:Convert

Revisi sejak 17 Oktober 2026 21.12 oleh Wikisporta (bicara | kontrib) (Melindungi "Modul:Convert" ([Sunting=Hanya untuk pengurus] (selamanya) [Pindahkan=Hanya untuk pengurus] (selamanya)))
(beda) ← Revisi sebelumnya | Revisi terkini (beda) | Revisi selanjutnya → (beda)

Dokumentasi untuk modul ini dapat dibuat di Modul:Convert/doc

local p = {}

-- Fungsi pembantu untuk membulatkan angka
local function round(val, decimal)
    if not val then return nil end
    local exp = 10 ^ (decimal or 0)
    return math.floor(val * exp + 0.5) / exp
end

function p.main(frame)
    local args = frame:getParent().args
    local val = tonumber(args[1] or args.value)
    local unitFrom = string.lower(args[2] or args.from or "")
    local unitTo = string.lower(args[3] or args.to or "")

    if not val then
        return '<span style="color:red; font-weight:bold;">[Nilai Angka Salah]</span>'
    end

    local resultStr = ""

    -- 1. Konversi Tinggi: Centimeter (cm) -> Kaki & Inci (ft/in)
    if unitFrom == "cm" and (unitTo == "ftin" or unitTo == "ft" or unitTo == "") then
        local totalInches = val / 2.54
        local feet = math.floor(totalInches / 12)
        local inches = round(totalInches % 12, 0)
        if inches == 12 then
            feet = feet + 1
            inches = 0
        end
        resultStr = string.format('%d cm <span style="white-space: nowrap;">(%d ft %d in)</span>', val, feet, inches)

    -- 2. Konversi Tinggi: Meter (m) -> Kaki & Inci (ft/in)
    elseif unitFrom == "m" and (unitTo == "ftin" or unitTo == "ft" or unitTo == "") then
        local cmVal = val * 100
        local totalInches = cmVal / 2.54
        local feet = math.floor(totalInches / 12)
        local inches = round(totalInches % 12, 0)
        if inches == 12 then
            feet = feet + 1
            inches = 0
        end
        local mStr = tostring(val):gsub("%.", ",")
        resultStr = string.format('%s m <span style="white-space: nowrap;">(%d ft %d in)</span>', mStr, feet, inches)

    -- 3. Konversi Berat: Kilogram (kg) -> Pound (lb)
    elseif unitFrom == "kg" and (unitTo == "lb" or unitTo == "lbs" or unitTo == "") then
        local lbs = round(val * 2.20462262185, 0)
        resultStr = string.format('%d kg <span style="white-space: nowrap;">(%d lb)</span>', val, lbs)

    -- 4. Konversi Berat: Pound (lb) -> Kilogram (kg)
    elseif unitFrom == "lb" or unitFrom == "lbs" then
        local kg = round(val / 2.20462262185, 0)
        resultStr = string.format('%d lb <span style="white-space: nowrap;">(%d kg)</span>', val, kg)

    else
        return '<span style="color:red; font-weight:bold;">[Satuan Tidak Dikenali]</span>'
    end

    return resultStr
end

return p