Jump to content

Module:ConvertCustom: Difference between revisions

From Shark's Hypothetical Weather
Created page with "local p = {} function p.main(frame) -- Get the input arguments from the template local args = frame.args -- Check if the first argument (pressure) is passed and valid local pressure = tonumber(args[1]) -- args[1] is the first parameter passed from the template if not pressure then return "Error: Invalid or missing pressure value." end -- Get significant figures (default is 4) for general calculations local sigfig = tonumber(args..."
 
No edit summary
Line 26: Line 26:
     -- Abbreviate if required
     -- Abbreviate if required
     if abbr == 'on' then
     if abbr == 'on' then
         return formattedPressure .. " mbar (hPa); " .. formattedInHg .. " inHg"
         return formattedPressure .. " mbar (hPa) " .. formattedInHg .. "; inHg"
     else
     else
         return formattedPressure .. " millibar (hectopascal); " .. formattedInHg .. " inches of mercury"
         return formattedPressure .. " millibar (hectopascal); " .. formattedInHg .. " inches of mercury"

Revision as of 18:21, 24 July 2025

Documentation for this module may be created at Module:ConvertCustom/doc

local p = {}

function p.main(frame)
    -- Get the input arguments from the template
    local args = frame.args

    -- Check if the first argument (pressure) is passed and valid
    local pressure = tonumber(args[1]) -- args[1] is the first parameter passed from the template
    if not pressure then
        return "Error: Invalid or missing pressure value."
    end

    -- Get significant figures (default is 4) for general calculations
    local sigfig = tonumber(args['sigfig']) or 4

    -- Get abbreviation setting (default is 'on')
    local abbr = args['abbr'] or 'on'

    -- Conversion from mbar to inHg
    local inHg = pressure * 0.02953

    -- Always format the pressure with 0 decimal places and inHg with 2 decimal places
    local formattedPressure = string.format("%d", pressure)  -- No decimal places for mbar
    local formattedInHg = string.format("%.2f", inHg)        -- 2 decimal places for inHg

    -- Abbreviate if required
    if abbr == 'on' then
        return formattedPressure .. " mbar (hPa) " .. formattedInHg .. "; inHg"
    else
        return formattedPressure .. " millibar (hectopascal); " .. formattedInHg .. " inches of mercury"
    end
end

return p