This Forum has been archived

Visit the new Forums
Forums: Index Support Requests Number to text
Fandom's forums are a place for the community to help other members.
To contact staff directly or to report bugs, please use Special:Contact.

50px-Replacement filing cabinet.svg

Note: This topic has been unedited for 478 days. It is considered archived - the discussion is over. Do not add to unless it really needs a response.

Can anyone build me a template to convert numbers into text format. For example, changing '65' to 'sixty-fifth'. I only need up to one hundred (but any more would be great!) Cheers.    ǝsʞpɐןǝ  (message wall)  13:48, 14/05/2012

Anyone?    ǝsʞpɐןǝ  (message wall)  17:57, 14/05/2012
Does it have to be so complicated? Wouldn't "1st", "2nd", "65th", "423rd" be enough? --  pecoes  18:21, May 14, 2012 (UTC) 
I already have a template which does that. I am specifically looking for a worded version, as I am using a template to write the introductory paragraph.    ǝsʞpɐןǝ  (message wall)  19:16, 14/05/2012

It doesn't sound terribly hard, but I need to know a bit more context. How exactly are you going to be using it? Sounds like a simple #switch: job, though. It's actually harder to go the other way, i.e. from 10th (or tenth) to 10.

Without additional context I'd say it would go something like this:
|100=one hundred

I don't know your coding expertise, so I'll explain that this is saying, "take the value of the variable indicated by {{{number}}} and convert it to a word based upon whatever that value is". So if you named this template {{convert}}, and you had a variable nambed {{{number}}} already in an infobox or template, then you would type {{convert|{{{number}}}}}} and it would work perfectly. But, again, I don't know what you're doing with it. czechout@fandom    fly tardis  <span style="">19:50: Mon 14 May 2012 

Post a link to your ordinal number template, please! Who knows - maybe can throw something together really quickly by adding a switch like the one CzechOut suggested to it...
 pecoes  20:02, May 14, 2012 (UTC) 
Cheers CzechOut. My coding is very good, but I was thinking too compicated. I didn't even consider that a switch with every number would be good.
Pecoes, my ordinal template is at w:c:eladkse:Template:Ordinal, but I don't think you need it now.    ǝsʞpɐןǝ  (message wall)  21:33, 14/05/2012
A not-so-smart idea came to mind.. What about this?
{{#switch:{{{1|}}}|10=ten|11=eleven|12=twelve|13=thirteen|14=fourteen|15=fifteen|16=sixteen|17=seventeen|18=eighteen|19=nineteen|100=one hundred|#default={{#ifexpr:{{#len:{{{1|}}}}}=2|{{#switch:{{#sub:{{{1|}}}|0|1}}|2=twenty|3=thirty|4=forty|5=fifty|6=sixty|7=seventy|8=eighty|9=ninety}}{{#ifeq:{{#sub:{{{1|}}}|-1|1}}|0||-}}}}{{#switch:{{#sub:{{{1|}}}|-1|1}}|1=one|2=two|3=three|4=four|5=five|6=six|7=seven|8=eight|9=nine|#default=}}}}
If you want this for 100+
{{#ifexpr:{{{1|}}}>=100|one hundred{{#ifexpr:{{{1|}}}>100| and }}|}}{{#switch:{{#sub:{{{1|}}}|-2|2}}
would work up to 199. If you'd like to limit usage to 199, wrap the code with {{#ifexpr:200>{{{1|}}}|...}}. By the way how to verify if the value is a natural number..? :S hm...  Cafeinlove msg 2012-05-15, 9:18 am(UTC+9)
Hmm... I'll have to look into the natural number bit, as I'll need to add it to my original template. Your code is closer to the one I originally wanted, but encounters the same problem which confused me to start with - how to get the correct ordinal all of the time. I think the basic switch will probably be the best way in te end, as it means less work.    ǝsʞpɐןǝ  (message wall)  05:54, 15/05/2012
It was a source for cardinal numbers so it could have some trouble if we change every parameter into ordinal ones. Anyway, if you need it for only 1 to 100, I also find #switch way simpler.  Cafeinlove msg 2012-05-15, 15:26 pm(UTC+9)
I got the idea for natural number issue. {{#ifexpr:{{{1|}}}-{{#expr:{{{1|}}} round0}}=0 and {{{1|}}}>0|...}} I just ran a test and it worked fine.  Cafeinlove msg 2012-05-15, 15:38 pm(UTC+9)
Well, I still think the basic switch is probably just fine for 1-100. But if you're looking for the "fancy", mathematical way of doing it, I think I can kinda extrapolate from having done mass conversions of ordinal to cardinal numbers at w:c:tardis. Basically, it's a matter of using div by 10 and mod by 10, and then thanking your lucky stars that English is "regular in its irregularities".
Let's examine the number 57. Look what happens with the following mathematical operations:
{{#expr: trunc (57 / 10)}} --> 5
{{#expr: (57 mod 10)}} -- > 7
So. If you divide by 10 and truncate, you get the first digit. If you mod by 10 you get the second digit. This then allows you to build a switch on the first digit — whereby you say "return all the the letters of the number before you get to the -ty suffix. So, 3 would give you "thir", 7 would give you "seven", etc.
You're then left with what to do about the other digit. So you do either a #switch or an #ifexpr on the value of (number mod 10). If the value is a 0 then you know it's 20, 30, 40, 50, etc. So you can then just add -ty. If it's other than 0 you then have to still attach -ty, but you'd have to figure out how to attach the full second numeral. So your other case would have a result of something like ty-.
Thinking about it, this "another template" is a third template which tells you the value of a number between 1 and 19, because what I've described only fully works for 20-99. If you wanted to go above 99, you'd build similar templates for every point where the English language changes its stripes. So you'd need a new template at 1k, 1mil, 1bil, 1 tril. Each time, it's just a matter of using div and trunc to decode the thing, and using the "more junior" templates to fill in the lower numbers. The process is metaphorically like Russian babushka dolls, with each template representing larger numbers having the template for the smaller range within it. By the time you got to the billions, you'd have a huge nest of #switches, each of them dependent on the "next-most-junior" template.
So, just to be super clear, for the template handling 1-99, you'd need a template for 1-19. For the template handling 100-999, you'd need templates for 1-19 and 20-99. And so on.
Sorry for not posting the entire code, but hopefully that plain text description will be a bit more helpful in the long run than a code dump. I'm sure I got the core notion of the div 10, mod 10 dynamic from wikipedia, but I can't remember at this point which specific template it was at. I remember the one I used the most was called {{ordinal}} so chances are the templates have a very literal naming system. czechout@fandom    fly tardis  <span style="">16:09: Tue 15 May 2012 
Yeah, I understood most of that, but I don't really think I need it. I've made a switch template and it works perfectly. I only needed 1-100 and I have that. Anything else is passed onto the other ordinal template. Cheers.    ǝsʞpɐןǝ  (message wall)  16:11, 15/05/2012