Module:Val/units

-- Definitions for units known to val (short scale; see below for long scale). -- There must be two spaces (not tab characters) after each unit code, and -- two spaces or tabs between columns. -- There must be a blank line before the first unit and a newline after the last. -- Unit code      Symbol                                Link                                   Flags local builtin_units = [=[

"                 ″                                     second of arc                          NOSPACE %                  %                                     percent                                NOSPACE '                  ′                                     minute of arc                          NOSPACE °                  °                                     degree (angle)                         NOSPACE ‰                  ‰                                     per mil                                NOSPACE ′                  ′                                     minute of arc                          NOSPACE ″                  ″                                     second of arc                          NOSPACE µA                 µA                                    microampere µB                 µB                         Bohr magneton µG                 µG                                    microgauss µGal              µGal                                  Gal (unit) µHz               µHz                                   hertz µK                µK                                    microkelvin µN                µN                         nuclear magneton µT                µT                                    microtesla µV                µV                                    microvolt μg/dL             μg/dL                                 gram per litre μg/dl             μg/dL                                 gram per litre μN                µN                         nuclear magneton A                 A                                     ampere aA                aA                                    attoampere AD                AD                                    Anno Domini aG                aG                                    attogauss ag                ag                                    attogram aHz               aHz                                   attohertz aK                aK                                    attokelvin am                am                                    attometre arcmin            ′                                     minute of arc                          NOSPACE arcminute         ′                                     minute of arc                          NOSPACE arcsec            ″                                     second of arc                          NOSPACE arcsecond         ″                                     second of arc                          NOSPACE aT                aT                                    attotesla BC                BC                                    Before Christ BCE               BCE                                   Before the Common Era bit/s             bit/s                                 bit per second BP                BP                                    Before present bps               bit/s                                 bit per second bya               bya                                   billion years ago byr               byr                                   billion years c                 c                              speed of light C                 C                                     coulomb cA                cA                                    centiampere CE                CE                                    Common Era cG                cG                                    centigauss cg                cg                                    centigram cHz               cHz                                   centihertz cK                cK                                    centikelvin cm-1              cm&minus;1                 wavenumber cT                cT                                    centitesla cV                cV                                    volt dA                dA                                    ampere daA               daA                                   decaampere daG               daG                                   decagauss dag               dag                                   decagram daHz              daHz                                  decahertz daK               daK                                   decakelvin daT               daT                                   decatesla daV               daV                                   volt deg               °                                     degree (angle)                         NOSPACE degC              °C                                    degree Celsius degF              °F                                    Fahrenheit degR              °R                                    Rankine scale dG                dG                                    decigauss dg                dg                                    decigram dHz               dHz                                   hertz dK                dK                                    decikelvin dT                dT                                    decitesla dV                dV                                    volt e                 e                              Elementary charge EA                EA                                    ampere Earth mass        M⊕                  Earth mass Earth radius      R⊕                  Earth radius EG                EG                                    exagauss Eg                Eg                                    exagram EHz               EHz                                   exahertz EK                EK                                    exakelvin ET                ET                                    exatesla EV                EV                                    volt eV/c2             eV/c2               electronvolt#Mass F                 F                                     farad fA                fA                                    femtoampere fb-1              fb&minus;1                 Barn (unit) fG                fG                                    femtogauss fg                fg                                    femtogram fHz               fHz                                   femtohertz fK                fK                                    femtokelvin fT                fT                                    femtotesla fV                fV                                    femtovolt g*mol-1           g&sdot;mol&minus;1         molar mass g/cm3             g/cm3                      gram per cubic centimetre g/l               g/l                                   gram per litre g/L               g/L                                   gram per litre g/mol             g&sdot;mol&minus;1         molar mass GA                GA                                    gigaampere Ga                Ga                                    gigaannum gal               gal                                   gallon Gbit/s            Gbit/s                                gigabit per second GeV/c2            GeV/c2              electronvolt#Mass GG                GG                                    gigagauss Gg                Gg                                    gigagram GHz               GHz                                   gigahertz GJ*mol-1          GJ&sdot;mol&minus;1        joule per mole GJ/mol            GJ/mol                                joule per mole GK                GK                                    gigakelvin GPa               GPa                                   gigapascal Gpc               Gpc                                   megaparsec#Megaparsecs and gigaparsecs Gpc2              Gpc2                       megaparsec#Megaparsecs and gigaparsecs Gpc3              Gpc3                       megaparsec#Megaparsecs and gigaparsecs GT                GT                                    gigatesla GV                GV                                    gigavolt Gyr               Ga                                    gigaannum H                 H                                     Henry (unit) H*m-1             H                                     Henry (unit) hA                hA                                    hectoampere hG                hG                                    hectogauss hg                hg                                    hectogram hHz               hHz                                   hectohertz hK                hK                                    hectokelvin hT                hT                                    hectotesla hV                hV                                    volt Hz                Hz                                    hertz J*mol-1           J&sdot;mol&minus;1         joule per mole J*s               J&sdot;s                              joule-second J/kg              J/kg                                  joule J/mol             J/mol                                 joule per mole Js                J&sdot;s                              joule-second Jupiter mass      MJ                  Jupiter mass Jupiter radius    RJ                  Jupiter radius kA                kA                                    kiloampere ka                ka                                    kiloannum kbit/s            kbit/s                                kilobit per second keV/c2            keV/c2              electronvolt#Mass kG                kG                                    kilogauss kg/cm3            kg/cm3                     density#Formula and common units kg/m3             kg/m3                      kilogram per cubic metre kg/mol            kg&sdot;mol&minus;1        molar mass kHz               kHz                                   Kilohertz kJ*mol-1          kJ&sdot;mol&minus;1        joule per mole kJ/mol            kJ/mol                                joule per mole kK                kK                                    kilokelvin KPa               kPa                                   pascal (unit) kpc               kpc                                   kiloparsec kpc2              kpc2                       kiloparsec kpc3              kpc3                       kiloparsec kT                kT                                    kilotesla kV                kV                                    kilovolt kW.h              kW&sdot;h                             kilowatt hour kWh               kWh                                   kilowatt hour kW·h              kW&sdot;h                             kilowatt hour kya               kya                                   kyr kyr               kyr                                   kyr L_solar           L&#x2609;           solar luminosity lbf               lbF                               Pound (force) lbm               lbm                               Pound (mass) ly                ly                                    light-year m                 m                                     metre m*s-1             m&sdot;s&minus;1           metre per second m*s-2             m&sdot;s&minus;2           metre per second squared m-1               m&minus;1                  metre m-2               m&minus;2                  square metre m-3               m&minus;3                  cubic metre m2                m2                         Square metre m3                m3                         Cubic metre M_Earth           M⊕                  Earth mass M_Jupiter         MJ                  Jupiter mass M_solar           M&#x2609;           solar mass M_Solar           M&#x2609;           solar mass MA                MA                                    megaampere Ma                Ma                                    megaannum mA                mA                                    milliampere Mbit/s            Mbit/s                                megabit per second mcg               µg                                    microgram mcg/dL            µg/dL                                 gram per mcg/dl            µg/dl                                 gram per meter             m                                     metre metre             m                                     metre MeV/c2            MeV/c2              electronvolt#Mass MG                MG                                    megagauss Mg                Mg                                    megagram mG                mG                                    milligauss mg/ml             mg/mL                                 gram per litre mg/mL             mg/mL                                 gram per litre mHz               mHz                                   hertz MHz               MHz                                   megahertz MJ*mol-1          MJ&sdot;mol&minus;1        joule per mole MJ/mol            MJ/mol                                joule per mole MK                MK                                    megakelvin mK                mK                                    millikelvin Mpc               Mpc                                   parsec#Megaparsecs and gigaparsecs Mpc2              Mpc2                       parsec#Megaparsecs and gigaparsecs Mpc3              Mpc3                       parsec#Megaparsecs and gigaparsecs MT                MT                                    megatesla mT                mT                                    millitesla muA               µA                                    ampere muG               µG                                    microgauss mug               µg                                    microgram muGal             µGal                                  Gal (unit) muHz              µHz                                   hertz muJ               µJ                                    joule muK               µK                                    microkelvin mum               µm                                    micrometre mus               µs                                    microsecond muT               µT                                    microtesla muV               µV                                    volt MV                MV                                    volt mV                mV                                    volt mya               mya                                   million years ago myr               myr                                   million years N*A-2             N&sdot;A&minus;2           permeability (electromagnetism) N*m-1             N&sdot;m&minus;1           joule (unit) nA                nA                                    ampere nG                nG                                    nanogauss ng                ng                                    nanogram nHz               nHz                                   hertz nK                nK                                    nanokelvin nT                nT                                    nanotesla nV                nV                                    volt pA                pA                                    ampere PA                PA                                    ampere pc2               pc2                        Parsec pc3               pc3                        Parsec per cent          %                                     percent                                NOSPACE per mil           ‰                                     per mil                                NOSPACE per mill          ‰                                     per mil                                NOSPACE per mille         ‰                                     per mil                                NOSPACE percent           %                                     percent                                NOSPACE permil            ‰                                     per mil                                NOSPACE permill           ‰                                     per mil                                NOSPACE permille          ‰                                     per mil                                NOSPACE PG                PG                                    petagauss Pg                Pg                                    petagram pG                pG                                    picogauss pg                pg                                    picogram pHz               pHz                                   hertz PHz               PHz                                   petahertz PK                PK                                    petakelvin pK                pK                                    picokelvin ppb               ppb                                   parts per billion ppm               ppm                                   parts per million PT                PT                                    petatesla pT                pT                                    picotesla PV                PV                                    volt pV                pV                                    volt R                 R                                     rayleigh (unit) R_Earth           R⊕                  Earth radius R_Jupiter         RJ                  Jupiter radius R_solar           R&#x2609;           solar radius R_Solar           R&#x2609;           solar radius s                 s                                     second s-1               s&minus;1                  second s-2               s&minus;2                  second s2                s2                         second second            s                                     second seconds           s                                     second solar luminosity  L&#x2609;           solar luminosity Solar luminosity  L&#x2609;           solar luminosity solar mass        M&#x2609;           solar mass Solar mass        M&#x2609;           solar mass solar radius      R&#x2609;           solar radius Solar radius      R&#x2609;           solar radius TA                TA                                    ampere Tbit/s            Tbit/s                                terabit per second TeV/c2            TeV/c2              electronvolt#Mass TG                TG                                    teragauss Tg                Tg                                    teragram THz               THz                                   terahertz TJ*mol-1          TJ&sdot;mol&minus;1        joule per mole TJ/mol            TJ/mol                                joule per mole TK                TK                                    terakelvin torr              Torr                                  Torr Torr              Torr                                  Torr TT                TT                                    teratesla TV                TV                                    volt u                 u                                     atomic mass unit uBP               14C yr BP                  Radiocarbon dating#Calibration USgal             US gal                                US gallon V                 V                                     volt V*m-1             V&sdot;m&minus;1           electric field V/m               V/m                                   electric field Vm-1              V&sdot;m&minus;1           electric field VV                VV                                    volt W                 W                                     Watt Wb                Wb                                    Weber (unit) y                 y                                     year YA                YA                                    ampere yA                yA                                    ampere year              year                                  year years             years                                 year yG                yG                                    yoctogauss yg                yg                                    yoctogram YG                YG                                    yottagauss Yg                Yg                                    yottagram yHz               yHz                                   hertz YHz               YHz                                   yottahertz yK                yK                                    yoctokelvin YK                YK                                    yottakelvin ym                ym                                    yoctometre yr                yr                                    year#Symbols y and yr yT                 yT                                    yoctotesla YT                YT                                    Yottatesla YV                YV                                    volt yV                yV                                    volt ZA                ZA                                    ampere zA                zA                                    ampere zG                zG                                    zeptogauss zg                zg                                    zeptogram ZG                ZG                                    zettagauss Zg                Zg                                    zettagram zHz               zHz                                   hertz ZHz               ZHz                                   hertz zK                zK                                    zeptokelvin ZK                ZK                                    zettakelvin zm                zm                                    zeptometre zT                zT                                    zeptotesla ZT                ZT                                    zettatesla zV                zV                                    volt ZV                ZV                                    volt

billiard          billiard                              Orders of magnitude (numbers)#1015 billion           billion                               1,000,000,000 billionth         billionth                             1,000,000,000 billionths        billionths                            1,000,000,000 decilliard        decilliard                            Orders of magnitude (numbers)#1063 decillion         decillion                             Orders of magnitude (numbers)#1033 decillionth       decillionth                           Orders of magnitude (numbers)#1033 decillionths      decillionths                          Orders of magnitude (numbers)#1033 milliard          milliard                              1,000,000,000 nonilliard        nonilliard                            Orders of magnitude (numbers)#1057 nonillion         nonillion                             Orders of magnitude (numbers)#1030 nonillionth       nonillionth                           Orders of magnitude (numbers)#1030 nonillionths      nonillionths                          Orders of magnitude (numbers)#1030 octilliard        octilliard                            Orders of magnitude (numbers)#1051 octillion         octillion                             Orders of magnitude (numbers)#1027 octillionth       octillionth                           Orders of magnitude (numbers)#1027 octillionths      octillionths                          Orders of magnitude (numbers)#1027 quadrilliard      quadrilliard                          Orders of magnitude (numbers)#1027 quadrillion       quadrillion                           Orders of magnitude (numbers)#1015 quadrillionth     quadrillionth                         Orders of magnitude (numbers)#1015 quadrillionths    quadrillionths                        Orders of magnitude (numbers)#1015 quintilliard      quintilliard                          Orders of magnitude (numbers)#1033 quintillion       quintillion                           Orders of magnitude (numbers)#1018 quintillionth     quintillionth                         Orders of magnitude (numbers)#1018 quintillionths    quintillionths                        Orders of magnitude (numbers)#1018 septilliard       septilliard                           Orders of magnitude (numbers)#1045 septillion        septillion                            Orders of magnitude (numbers)#1024 septillionth      septillionth                          Orders of magnitude (numbers)#1024 septillionths     septillionths                         Orders of magnitude (numbers)#1024 sextilliard       sextilliard                           Orders of magnitude (numbers)#1039 sextillion        sextillion                            Orders of magnitude (numbers)#1021 sextillionth      sextillionth                          Orders of magnitude (numbers)#1021 sextillionths     sextillionths                         Orders of magnitude (numbers)#1021 trilliard         trilliard                             Orders of magnitude (numbers)#1021 trillion          trillion                              Orders of magnitude (numbers)#1012 trillionth        trillionth                            Orders of magnitude (numbers)#1012 trillionths       trillionths                           Orders of magnitude (numbers)#1012

]=]

-- If val has "|long scale=on" the following definitions are used -- (if not found here, the normal definitions are used). -- Unit code      Symbol                                Link                                   Flags local builtin_units_long_scale = [=[

billion           billion                               Orders of magnitude (numbers)#1012 billionth         billionth                             Orders of magnitude (numbers)#1012 billionths        billionths                            Orders of magnitude (numbers)#1012 decillion         decillion                             Orders of magnitude (numbers)#1060 decillionth       decillionth                           Orders of magnitude (numbers)#1060 decillionths      decillionths                          Orders of magnitude (numbers)#1060 nonillion         nonillion                             Orders of magnitude (numbers)#1054 nonillionth       nonillionth                           Orders of magnitude (numbers)#1054 nonillionths      nonillionths                          Orders of magnitude (numbers)#1054 octillion         octillion                             Orders of magnitude (numbers)#1048 octillionth       octillionth                           Orders of magnitude (numbers)#1048 octillionths      octillionths                          Orders of magnitude (numbers)#1048 quadrillion       quadrillion                           Orders of magnitude (numbers)#1024 quadrillionth     quadrillionth                         Orders of magnitude (numbers)#1024 quadrillionths    quadrillionths                        Orders of magnitude (numbers)#1024 quintillion       quintillion                           Orders of magnitude (numbers)#1030 quintillionth     quintillionth                         Orders of magnitude (numbers)#1030 quintillionths    quintillionths                        Orders of magnitude (numbers)#1030 septillion        septillion                            Orders of magnitude (numbers)#1042 septillionth      septillionth                          Orders of magnitude (numbers)#1042 septillionths     septillionths                         Orders of magnitude (numbers)#1042 sextillion        sextillion                            Orders of magnitude (numbers)#1036 sextillionth      sextillionth                          Orders of magnitude (numbers)#1036 sextillionths     sextillionths                         Orders of magnitude (numbers)#1036 trillion          trillion                              Orders of magnitude (numbers)#1018 trillionth        trillionth                            Orders of magnitude (numbers)#1018 trillionths       trillionths                           Orders of magnitude (numbers)#1018

]=]

local function get_builtin_unit(unitcode, definitions) -- Return table of information for the specified built-in unit, or nil if not known. -- Each defined unit code must be followed by two spaces (not tab characters). local _, pos = definitions:find('\n' .. unitcode .. ' ', 1, true) if pos then local endline = definitions:find('\n', pos, true) if endline then local result = {} local n = 0 local text = definitions:sub(pos, endline - 1) for item in (text .. ' '):gmatch('(%S[^\n]-)%s%s') do				if item == 'NOSPACE' then result.nospace = true else n = n + 1 if n == 1 then result.symbol = item elseif n == 2 then result.link = item else break end end end if n == 2 then return result end -- Ignore invalid definition. end end end

local function get_unit(ucode, value, want_link, want_longscale) local result = want_longscale and get_builtin_unit(ucode, builtin_units_long_scale) or		get_builtin_unit(ucode, builtin_units) local lookup = require('Module:Convert/sandbox')._unit local convert_unit = lookup(ucode, { value = value, link = want_link }) if result then -- Have: result.symbol + result.link + result.nospace if want_link then result.text =  .. result.symbol ..  else result.text = result.symbol end result.sortkey = convert_unit.sortkey else result = { text = convert_unit.text, sortkey = convert_unit.sortkey, }	end return result end

local function bracketed(ucode, text) if ucode:find('[*./]') then return '(' .. text .. ')' end return text end

local function makeunit(ucode, options) -- Return wikitext, sortkey for the requested unit and options. -- LATER The sortkey does not account for any per unit. options = options or {} local unit = get_unit(ucode, options.value, options.link, options.longscale) local text = unit.text local percode = options.per if percode then local perunit = get_unit(percode, 0, options.per_link, options.longscale) text = bracketed(ucode, text) .. '/' .. bracketed(percode, perunit.text) end if not unit.nospace then text = ' ' .. text end return text, unit.sortkey end

return makeunit