Forum:Number to text

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.


 * Anyone?


 * Does it have to be so complicated? Wouldn't "1st", "2nd", "65th", "423rd" be enough? --


 * 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.

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: I don't know your coding expertise, so I'll explain that this is saying, "take the value of the variable indicated by and convert it to a word based upon whatever that value is". So if you named this template, and you had a variable nambed already in an infobox or template, then you would type } and it would work perfectly. But, again, I don't know what you're doing with it. 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...


 * 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.


 * A not-so-smart idea came to mind.. What about this?


 * If you want this for 100+




 * would work up to 199. If you'd like to limit usage to 199, wrap the code with . By the way how to verify if the value is a natural number..? :S hm...
 * 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.
 * 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.
 * I got the idea for natural number issue.  I just ran a test and it worked fine.


 * 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:
 *   -->
 *   -- >


 * 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.


 * 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.   <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.