To use the module from normal wiki pages, no special preparation is needed. If you are using the module from another Lua module, first you need to load it, like this:
localmf=require('Module:Formatnum')
(The mf variable stands for Module Formatnum; you can choose something more descriptive if you prefer.)
Most functions in the module have a version for Lua and a version for #invoke. It is possible to use the #invoke functions from other Lua modules, but using the Lua functions has the advantage that you do not need to access a Lua frame object. Lua functions are preceded by _, whereas #invoke functions are not.
-- This module is intended to replace the functionality of Template:Formatnum and related templates. localp={}functionp.main(frame)localargs=frame:getParent().argslocalprec=args.precor''localsep=args.sepor''localnumber=args[1]orargs.numberor''locallang=args[2]orargs.langor''-- validate the language parameter within MediaWiki's caller frameiflang=="arabic-indic"then-- only for back-compatibility ("arabic-indic" is not a SupportedLanguage)lang="fa"-- better support than "ks"elseiflang==''ornotmw.language.isSupportedLanguage(lang)then-- Note that 'SupportedLanguages' are not necessarily 'BuiltinValidCodes', and so they are not necessarily-- 'KnownLanguages' (with a language name defined at least in the default localisation of the local wiki).-- But they all are ValidLanguageCodes (suitable as Wiki subpages or identifiers: no slash, colon, HTML tags, or entities)-- In addition, they do not contain any capital letter in order to be unique in page titles (restriction inexistant in BCP47),-- but they may violate the standard format of BCP47 language tags for specific needs in MediaWiki.-- Empty/unspecified and unsupported languages are treated here in Commons using the user's language,-- instead of the local 'ContentLanguage' of the Wiki.lang=frame:callParserFunction("int","lang")-- get user's chosen languageendreturnp.formatNum(number,lang,prec,sep~='')endlocaldigit={-- substitution of decimal digits for languages not supported by mw.language:formatNum() in core Lua libraries for MediaWiki["ml-old"]={'൦','൧','൨','൩','൪','൫','൬','൭','൮','൯'},["mn"]={'᠐','᠑','᠒','᠓','᠔','᠕','᠖','᠗','᠘','᠙'},["ta"]={'௦','௧','௨','௩','௪','௫','௬','௭','௮','௯'},["te"]={'౦','౧','౨','౩','౪','౫','౬','౭','౮','౯'},["th"]={'๐','๑','๒','๓','๔','๕','๖','๗','๘','๙'}}functionp.formatNum(number,lang,prec,compact)-- Do not alter the specified value when it is not a valid number, return it as islocalvalue=tonumber(number)ifvalue==nilthenreturnnumberend-- Basic ASCII-only formatting (without paddings)number=tostring(value)-- Check the presence of an exponent (incorrectly managed in mw.language:FormatNum() and even forgotten due to an internal bug, e.g. in Hindi)localexponentlocalpos=string.find(number,'[Ee]')ifpos~=nilthenexponent=string.sub(number,pos+1,string.len(number))number=string.sub(number,1,pos-1)elseexponent=''end-- Check the minimum precision requestedprec=tonumber(prec)-- nil if not specified as a true numberifprec~=nilthenprec=math.floor(prec)ifprec<0thenprec=nil-- discard an incorrect precision (not a positive integer)elseifprec>14thenprec=14-- maximum precision supported by tostring(number)endend-- Preprocess the minimum precision in the ASCII stringlocaldotif(precor0)>0thenpos=string.find(number,'.',1,true)-- plain search, no regexpifpos~=nilthenprec=pos+prec-string.len(number)-- effective number of trailing decimals to add or removedot=''-- already presentelsedot='.'-- must be addedendelsedot=''-- don't add dotprec=0-- don't alter the precisionendiflang~=nilandmw.language.isKnownLanguageTag(lang)==truethen-- Convert number to localized digits, decimal separator, and group separatorslocallanguage=mw.getLanguage(lang)ifcompactthennumber=language:formatNum(tonumber(number),{noCommafy='y'})-- caveat: can load localized resources for up to 20 languageselsenumber=language:formatNum(tonumber(number))-- caveat: can load localized resources for up to 20 languagesend-- Postprocessing the precisionifprec>0thenlocalzero=language:formatNum(0)number=number..dot..mw.ustring.rep(zero,prec)elseifprec<0then-- TODO: rounding of last decimal; here only truncate decimals in excessnumber=mw.ustring.sub(number,1,mw.ustring.len(number)+prec)end-- Append the localized base-10 exponent without grouping separators (there's no reliable way to detect a localized leading symbol 'E')ifexponent~=''thennumber=number..'E'..language:formatNum(tonumber(exponent),{noCommafy=true})endelse-- not localized, ASCII only-- Postprocessing the precisionifprec>0thennumber=number..dot..mw.string.rep('0',prec)elseifprec<0then-- TODO: rounding of last decimal; here only truncate decimals in excessnumber=mw.string.sub(number,1,mw.string.len(number)+prec)end-- Append the base-10 exponentifexponent~=''thennumber=number..'E'..exponentendend-- Special cases for substitution of ASCII digits (missing support in Lua core libraries for some languages)ifdigit[lang]thenfori,vinipairs(digit[lang])donumber=mw.ustring.gsub(number,tostring(i-1),v)endendreturnnumberendreturnp