Jump to content

Module:ConvertSeasonPressure: 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
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


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


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


     -- Get significant figures (default is 4) for general calculations
     -- Optional abbr flag
    local sigfig = tonumber(args['sigfig']) or 4
 
    -- Get abbreviation setting (default is 'on')
     local abbr = args['abbr'] or 'on'
     local abbr = args['abbr'] or 'on'


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


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


    -- Abbreviate if required
     if abbr == 'on' then
     if abbr == 'on' then
         return formattedPressure .. " [[wikipedia:Bar_(unit)|mbar]] ([[wikipedia:Pascal_(unit)|hPa]]); " .. formattedInHg .. " [[wikipedia:Inch_of_mercury|inHg]]"
         return formattedPressure ..
            " [[wikipedia:Bar_(unit)|mbar]] " ..
            "([[wikipedia:Pascal_(unit)|hPa]]; " ..
            formattedInHg .. " [[wikipedia:Inch_of_mercury|inHg]])"
     else
     else
         return formattedPressure .. " millibar (hectopascal); " .. formattedInHg .. " inches of mercury"
         return formattedPressure ..
            " millibar (hectopascal; " ..
            formattedInHg .. " inches of mercury)"
     end
     end
end
end


return p
return p

Latest revision as of 19:37, 2 ⧼october⧽ 2025

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

local p = {}

function p.main(frame)
    local args = frame:getParent().args -- use template args

    -- First argument = pressure (mbar)
    local pressure = tonumber(args[1])
    if not pressure then
        return "Error: Invalid or missing pressure value."
    end

    -- Optional abbr flag
    local abbr = args['abbr'] or 'on'

    -- Convert mbar → inHg
    local inHg = pressure * 0.02953

    -- Format
    local formattedPressure = string.format("%d", pressure)
    local formattedInHg = string.format("%.2f", inHg)

    if abbr == 'on' then
        return formattedPressure ..
            " [[wikipedia:Bar_(unit)|mbar]] " ..
            "([[wikipedia:Pascal_(unit)|hPa]]; " ..
            formattedInHg .. " [[wikipedia:Inch_of_mercury|inHg]])"
    else
        return formattedPressure ..
            " millibar (hectopascal; " ..
            formattedInHg .. " inches of mercury)"
    end
end

return p