Module talk:ConvertNumeric
numeral_to_english
Converts a given integer to its English text representation. Accepts integers (including negative and zero), decimal numbers, and scientific notation. Any value not matching these is evaluated as an expression and the result is used. Any value with magnitude less than 1000 quadragintillion (10126) is supported, as well as some larger numbers such as one centillion (10303) and one millinillion (103003).
Examples
{{#invoke:ConvertNumeric | numeral_to_english | -123456789.25 }}
- negative one hundred twenty-three million four hundred fifty-six thousand seven hundred and eighty-nine point two five
{{#invoke:ConvertNumeric | numeral_to_english | 5E30 | case=u | ord=on | lk=on }}
- Five nonillionth
{{#invoke:ConvertNumeric | numeral_to_english | 57000 | round=on | plural=on}}
- sixty thousands
{{#invoke:ConvertNumeric | numeral_to_english | 35 + 42 | adj=on }}
- seventy-seven
{{#invoke:ConvertNumeric | numeral_to_english | 3+1/2 }}
- three point five
{{#invoke:ConvertNumeric | numeral_to_english | 3 | numerator = 1 | denominator = 2 }}
- three and a half
Optional parameters
- numerator
- denominator
- both necessary for fractions
- case
- Set to "U" or "u" to capitalize result
- sp
- Set to "us" to remove "and" between hundreds and tens places
- adj
- Hyphenate
- ord
- Ordinal (e.g. first instead of one)
- pl
- Plural
- lk
- If set to 'on', will link the words "billion" and larger to their respective short-scale article sections. This might be helpful for disambiguation. Subsets of words can be linked to by specifying them, e.g. lk=trillion will link only "trillion", and lk=trillion,quadrillion will link only those two.
- negative
- Sets the word to use for negative if the supplied value is negative (e.g. negative or minus)
- round
- If set to 'on', will round the number to the nearest one- or two-word integer. |round=down and |round=up are similar but round down and up respectively.
decToHex
This takes a string and converts all the decimal sequences in it to hex.
{{#invoke:ConvertNumeric|decToHex|10 20 40 1024 78912345|minlength=4}}
produces
000A 0014 0028 0400 4B41B59
roman_to_numeral
Hi, I wanted to use this function in a module I'm working on (Module:Sandbox/Bility/SortName), but it doesn't appear to be exposed publicly. Is that right, or could I just not figure out how to access it using require ('Module:ConvertNumeric')
? For now I've copy/pasted it into my module. — Bility (talk) 22:23, 11 April 2013 (UTC)
Make more functions public
Would be good if the functions for numeral_to_english_less_1000
and numeral_to_english_less_100
be made public (maybe the others as well), as currently to access them from another module I need to do this abomination - .spell_number(num, nil, nil, false, false, false, true, false, nil, nil, nil, nil, false)
(just to get the true
value included; can ignore the last 6 in my personal example, but maybe not in other uses), which I get by using the public method _numeral_to_english
It also does a lot of other unnecessary code that is not relevant for my specific needs of converting a number into English. --Gonnym (talk) 14:05, 21 September 2018 (UTC)
- OK but for completeness you might indicate where this would be used and briefly why it is not integrated into this module. Johnuniq (talk) 01:33, 22 September 2018 (UTC)
- Sorry @Johnuniq:, did not have this on my watchlist and didn't see you responded. For my specific use-case (Module:Television episode short description), I need to get a number converted to an ordinal word in English. I call this function from another module as this is just the conversion part of the code, not the actual work the module does. For me the only thing I really need is a direct access to
numeral_to_english_less_1000
(to be more specific to the 100, as I'll never need a number bigger than 100, but I can live with that few extra code lines) and to pass it only 2 variables - a number and true for ordinal. So a public function ofnumeral_to_ordinal_less_1000
that takes a number would be exactly what I need (and it can then pass tonumeral_to_english_less_1000
the rest of the values it needs). Another issue that I have with the current flow, is that I cannot send the number as a number, but as a text, as it fails atscientific_notation_to_decimal
. Hope I answered your questions. --Gonnym (talk) 10:16, 3 November 2018 (UTC)- @Gonnym: I am happy to fix this and I agree the interface is too complex and fragile. I think
spell_number
should continue to work as it does now so nothing breaks, but should also acceptspell_number(num, options)
whereoptions
is a table. For example,spell_number(num, {ordinal=true})
could be used. However, I don't want to take the time to decode what Module:Television episode short description is doing and I haven't looked at Module:ConvertNumeric for a very long time so have forgotten why access to functions like numeral_to_english_less_1000 might be helpful. A very quick look makes me think your module needs things like translating 1 to 1st and 12 to twelfth. Would passing a table as just described do all you need? Why does your module have getOrdinalIndicatorLessThan1000 and related logic? I would have thought that spell_number should do all that is needed. Johnuniq (talk) 22:47, 3 November 2018 (UTC)- @Johnuniq:, I was actually thinking of bypassing that whole section, and adding to local p = {} a new function numeral_to_english_ordinal_less_1000() which will be something like this:
- @Gonnym: I am happy to fix this and I agree the interface is too complex and fragile. I think
- Sorry @Johnuniq:, did not have this on my watchlist and didn't see you responded. For my specific use-case (Module:Television episode short description), I need to get a number converted to an ordinal word in English. I call this function from another module as this is just the conversion part of the code, not the actual work the module does. For me the only thing I really need is a direct access to
local function numeral_to_english_ordinal_less_1000(num)
if (tonumber(num) ~= nil) then
if (num < 1000) then
return numeral_to_english_less_1000(num, false, true, false, nil)
else
return "" (code that deals with bigger numbers, where-ever that is)
end
else
return error("Invalid number")
end
end
Regarding my uses, you are correct. I use this module to convert a number to to an ordinal in the style of "twelfth". I have the additional logic in my code since as far as I can tell, this module does not convert a number to the style I needed. --Gonnym (talk) 06:44, 4 November 2018 (UTC)