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.

DarkBASIC Professional Discussion / Would like to numbers into words?

Author
Message
HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 27th Jul 2010 16:27
Has anyone done this, would like to take 1234 or 56000 or 400 or 30 and write it out as

one thousands two hundred and thirty four
fifty six thousands
four hundred
thirty

any number from 1 to 9-10 million and make them words.

this what I have but it does work like i thought is should.


Dark Physics makes any hot drink go cold.
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 27th Jul 2010 16:34
It seems like you are on your way to do that already, but just in case.... You would have to make a database of the spelling of each digit 0-9 and each place 0="" ,1="tens", 2="hundreds"..... and so on.

The fastest code is the code never written.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Jul 2010 16:43
First, divide up the number into chunks of 3 digits each, starting from the right hand side.

Each one except the first will have a unique word.
1 - [Nothing]
2 - thousand
3 - million
4 - billion
etc.

Take each triplet in turn. If the first digit is not zero, convert that digit to it's word (ie. 1 -> "one") and add ' hundred' to it.

If either of the next to digits is non-zero, add ' and' to the text for the current triplet.

For 1 to 19, you must do each case separately, for 20 to 99, you can convert the two digits separately (ie. 21 -> ' twenty' + ' one'). Add this to the current triplet.

Next, append the word for the current triplet (thousand/million/billion/etc.) to it.

Then you just need to concatenate all the triplets together to get the final number. Add in the word ' and' before the last one if it is non-zero.

6124012 -> six million one hundred and twenty four thousand and twelve

[b]
HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 27th Jul 2010 16:45
I see what do when it three digits I know it be hundreds.
I can't see how to get the 112 or 673 in to words.

I can see that there will be one to ten
I can also see there will be eleven to nineteen
and 20 30 40 50 60 70 80 90 + 1-9

I see what to do, but not the logic to get there.

Dark Physics makes any hot drink go cold.
HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 27th Jul 2010 16:49
Thanks Diggsey , was writing my reply when you posted yours, makes a it bit clearer, and a way to start. cheers.

Dark Physics makes any hot drink go cold.
HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 27th Jul 2010 18:58 Edited at: 27th Jul 2010 18:59
Ok this works but I cannot see how to simplify it, need to go to thousands and millions but with the way I am doing it I will end repeat the same code again again, tried turning upside down so it did the last one first but it did not work.

could some have a look at it and see if they one work out what I've done and two swap it a round.




Dark Physics makes any hot drink go cold.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Jul 2010 21:28
This should work:



[b]
Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 27th Jul 2010 22:12
I just refreshed the page; looks like Diggsey beat me to it . I added the numbers (billion, trillion, etc.) that he has in there, but DBP has problems with large numbers so it might be for naught.

Anyway here's another number to name conversion function (with hyphens!):



And here's a function to add commas to your numbers (makes numbers look nicer )



Finally, here's an example:



Guns, cinematics, stealth, items and more!
HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 27th Jul 2010 22:38
Absolutely brilliant, I understand a bit of what it does, but some of it will take studying.

Its so good I hope you are going to post in the code snippets

Cheers Diggsey .

Dark Physics makes any hot drink go cold.
HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 27th Jul 2010 22:42
cheers Sixty Squares good bit of code, like you said pipped by someone else, well I may end up using yet like at the end of game to say how much lost and won.

Dark Physics makes any hot drink go cold.

Login to post a reply

Server time is: 2026-07-25 17:05:45
Your offset time is: 2026-07-25 17:05:45