Module talk:ConvertNumeric

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

numeral_to_english[edit]

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[edit]

  • {{#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[edit]

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[edit]

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[edit]

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)[reply]

Make more functions public[edit]

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)[reply]

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)[reply]
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 of numeral_to_ordinal_less_1000 that takes a number would be exactly what I need (and it can then pass to numeral_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 at scientific_notation_to_decimal. Hope I answered your questions. --Gonnym (talk) 10:16, 3 November 2018 (UTC)[reply]
@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 accept spell_number(num, options) where options 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)[reply]
@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:
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

The code above could use your idea for the options table if ordinal can be used along with another param (haven't checked to see). 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)[reply]

@Gonnym: I'm wondering why you are thinking of that function. Any change to this module should only occur after working out if something is missing. Please give an example of input and output showing results from your code and this module to show the style issue. Are you saying this module cannot turn '12' into 'twelfth'? Johnuniq (talk) 07:05, 4 November 2018 (UTC)[reply]
That isn't what I said. I said that I use this module to convert 12 to twelfth, but 1 to 1st, as far as I can tell, it doesn't. --Gonnym (talk) 07:07, 4 November 2018 (UTC)[reply]
OK, so the problem is that you need "1st" but the module only generates "first". I'll think about that and will ping you in a couple of days. Johnuniq (talk) 08:38, 4 November 2018 (UTC)[reply]
Well, technically, I don't need anything anymore as I've got everything sorted, but if this module did have this, it might be useful to others. But that really wasn't my point I was trying to make. I believe that the current entry point function is flawed from a coding perspective. The main function is around 100 lines long and does so many things that its a nightmare for readability and maintenance (and as I pointed out, if I pass a number which is a number, it fails). Some of the parameters are also mutually exclusive (such as plural and ordinal) so having them both as arguments in the same function doesn't serve anything. Instead, I would argue that having several entry points based on those mutually exclusive parameters would be a better design approach and would both allow for better readability, maintenance and future expansions (such as this). --Gonnym (talk) 11:14, 4 November 2018 (UTC)[reply]

Feature request[edit]

It would be nice if numeral_to_english (or an equivalent function) could do a combination of numerals and words. For example, 15×1012 would be rendered as "15 trillion". Take this a step further with decimal places: 15.1 trillion. Thanks. howcheng {chat} 00:24, 7 November 2018 (UTC)[reply]

Is there an example of an article where it would be helpful to use a template to turn "×1012" into "trillion"? I would find it easier to type the latter. Johnuniq (talk) 02:48, 7 November 2018 (UTC)[reply]
@Johnuniq: Sorry, I didn't mean literally "15×1012", but "15000000000000". And I'm mostly thinking of a number that large that comes as a result of a calculation. For example, in the introduction of 2016 Indian banknote demonetisation, we have The banknotes that were not deposited were only worth ₹10,720 crore (102.7 billion). The Western equivalent was not there until yesterday, and I made the template {{crore}} to do the conversion, which ends up as 107,200,000,000. I can then wrap with {{number to word}}, but it comes out as "one hundred seven billion two hundred million", when "102.7 billion" is easier to understand. howcheng {chat} 16:53, 7 November 2018 (UTC)[reply]
I've added that to a to do list but I've got occupied elsewhere and am not likely to do anything for quite a while. I'll ping you if there is good news. If implemented, I suppose it should be called with a dedicated template that produces thousand/million/billion/trillion rather than adding yet another option to {{number to word}}. Johnuniq (talk) 00:19, 8 November 2018 (UTC)[reply]
Fair enough. If I knew Lua syntax, I'd be happy to do it myself. howcheng {chat} 17:19, 8 November 2018 (UTC)[reply]

US parameter of Template:Number to word[edit]

Is the us parameter of {{number to word}} working correctly? The output does not seem to say "nine hundred and ninety-nine thousand" (per this) either with the parameter or without:

  • {{Number to word|999999999999}} nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred and ninety-nine
  • {{Number to word|999999999999|us=on}} nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine

Thanks, It Is Me Here (talk) 11:05, 10 April 2020 (UTC)[reply]

I'm seeing "and" without the parameter, as the doc says it should work. --Gonnym (talk) 11:10, 10 April 2020 (UTC)[reply]
Thanks for the reply. For me, the output text reads "nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred and ninety-nine" in both cases. Are you saying that you see "nine hundred and ninety-nine billion"? It Is Me Here (talk) 13:08, 10 April 2020 (UTC)[reply]
Unless there is a specific issue with a very long number, use short examples as it's easier to read. Here I see "and" in the first and no "and" in the second.
--Gonnym (talk) 13:51, 10 April 2020 (UTC)[reply]
  • {{Number to word|999999}} nine hundred ninety-nine thousand nine hundred and ninety-nine
Okay, this is a shorter example. My question is whether you see an "and" in the number of thousands. I agree I also see an "and" in the number of units. It Is Me Here (talk) 15:37, 10 April 2020 (UTC)[reply]
Ok, I now understand what you are asking. I do not see. Should it though? {{Number to word}} links to English numerals which gives an example of 999 - nine hundred and ninety-nine thousand (inclusively British English, Irish English, Australian English, and New Zealand English) / nine hundred ninety-nine thousand (American English) - notice that the American English example does not have "and". Since I'm not a linguistic or familiar with this topic, this is the most I can help you. --Gonnym (talk) 15:44, 10 April 2020 (UTC)[reply]
Another angle would be to ask whether it matters. Those of us who wrote enormous programs to generate lists of roman numerals as a training exercise would care, but is there any use-case at Wikipedia where the examples matter? Johnuniq (talk) 00:00, 11 April 2020 (UTC)[reply]

Template-protected edit request on 25 March 2021[edit]

Copy changes from sandbox. This will make it possible to access all of the normalization features in modules that was previously only available to templates. For example, p.numeral_to_english_full{' 99 '} will return "ninety-nine" whereas p.spell_number(' 99 ') will crash. User:GKFXtalk 18:17, 25 March 2021 (UTC)[reply]

@GKFX: The module is a bit of a mess that I have been intending to clean for a couple of years. That change looks good but I have some questions since I'd prefer to avoid adding more incompatability.
I'm too lazy to test your above statement. Are you saying that if a module calls this module with p.spell_number(' 99 ') then a crash occurs? If so, that should be fixed.
What does this comment mean? For use by Lua modules for compatibility with Template:Parse number et al.
What is "_full" in numeral_to_english_full? I can see that it takes a table as the parameter, but no other function has a convention where "_full" is used for that.
Johnuniq (talk) 04:29, 26 March 2021 (UTC)[reply]
@Johnuniq: Ah right I see. It looks like a nice module to me anyway! In order of your questions: yes it does crash because all the string cleanup is in (what I had called) numeral_to_english_full rather than _numeral_to_english. It would arguably be fine to just move that to _numeral_to_english. As for the word "full" it means that the function is a complete replacement for Template:Parse number rather than a fragile one which can't handle whitespace.
I have made another edit to the sandbox which makes spell_number and spell_number2 handle whitespace directly so p.spell_number(' 99 ') and p.spell_number2{ num = ' 99 ' } are now fine, rather than adding a new function. User:GKFXtalk 11:31, 26 March 2021 (UTC)[reply]
Thanks, that's greatly improved the code and has motivated me to look at my old notes to see what else should be done. I did a minor edit to the sandbox but plan to investigate a couple of other issues before this goes live. I'll also want to remind myself how testing is done. Johnuniq (talk) 04:29, 27 March 2021 (UTC)[reply]
BTW I'm hoping you will check my edits. In particular diff. Am I missing something or have those three regexes always had a redundant (wrong) component? Johnuniq (talk) 09:14, 27 March 2021 (UTC)[reply]
Yes I would agree that 12.3-E4 is not a legitimate way to write exponentials and that your edits are fine. User:GKFXtalk 13:21, 27 March 2021 (UTC)[reply]