# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### Code Snippets / [DBP] - String Manipulation: Plain-English Word Returns

Message
Posted: 17th Nov 2012 23:34
So, a program I used to own for creating mockups of MTG cards once had a function that would return the words written out, I.E. "5" becomes "five" and so forth. A text-based game I once played used a similar system, but only returned numbers less than 100 (due to the way it was structured, it never needed to)

Wanting a similar system for my own games, I've developed a function that will return the grammatically correct, English equivalent of any number beneath 9999. (seeing as very few are prone to write numbers out like this, my grammar in the thousands may be a bit off, if so, cite your source and I'll bug-fix it.)

If you catch any bugs (beyond the aforementioned fact that 10,000 or beyond returns "ERR thousand" or somesuch) report here, pleasles.

This could easily be used as the framework for any similar number-word system, i.e. English equivalence for currency values.

Posted: 18th Nov 2012 00:24
If you can handle 0 through 999, then you should be able to handle any number, 10,000 and beyond. Just break it down into segments of 3 digits each, run it through the 0-999 function to write it out, then add the word for whatever placement it's at.

23,463,243

twenty three (million )
four hundred sixty three (thousand)
two hundred forty three

I'd probably work from right to left. Every three digits, cut it off and run it through the function.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Posted: 18th Nov 2012 19:18
Unfortunately, neither the numbers or the words are written right to left.

Yeah, you have a point. Still, this framework should suffice. It's all I needed. If someone else needs something else, they can figure it out themselves.
Posted: 20th Nov 2012 05:20 Edited at: 20th Nov 2012 05:53
In your example of 1550, you say "five hundred AND fifty". Technically, you only say 'and' when referring to the decimal or fraction. I had a math teacher that used to yell at us for that.

I thought this made a neat little challenge, so I had a go at it myself.

I'm not trying to show up your code or anything, just thought tackling it myself from another approach would be fun.

I ran a speed test, mainly to see if the extra overhead of using my 4 arrays was worth it. I ran a loop calling the function from 1 to 9999 (cause you said yours wouldn't do 10k and up). Mine completes in 50ms. I'm afraid to say yours was, well, a tad higher. Yours starts out just fine, but I found the larger the number the longer it takes. 1 through 99 took only 16ms, but 8999 (just a single number, not a range) took 37ms. And 9999 itself took 42ms.

I'm looking at your code now to see if I can tell why larger numbers have such a higher latency, but I bet it has something to do with the while loops.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Posted: 20th Nov 2012 06:01 Edited at: 20th Nov 2012 06:09
Ok, I figured out why yours takes so long with larger numbers. You're using WHILE loops to actually count up the quantity at each place in the number

For example:

You can get the exact same results doing this:

If you replace all three of your WHILE loops with this code:

Then your code will complete 1 through 9999 in only 18ms. Ugh, yours is twice as fast as mine now! FML, I'm not helping anymore!

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Posted: 20th Nov 2012 23:46 Edited at: 20th Nov 2012 23:50
It's okay Phaelax.

Your code? You mean the one for working larger values?

And yeah, I knew the while loops were probably not the best idea, but I always keep hearing about risks whenever you divide anything by anything. Thanks for taking the risks and making the fix.

You're awesome Phaelax, faster code or no.
Posted: 10th Dec 2012 00:32
The changes made by Phaelax aren't risky.

It's always risky to divide by a variable, because you will have to make sure that the value of said variable is NEVER 0 ( zero ).

But in Phaelax's edited code he's deviding by constant values of 10.

That shouldn't be risky at all.

You can for example calculate 0 / 10 without a problem.
Posted: 10th Dec 2012 18:31
@Mr909,

Can you post the final code - will be v handy for me too in a project I'm doing...

a long time dabbler with DBC and DBPro with no actual talent but lots of enthusiasm...