Re: int to string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wednesday 05 April 2006 16:47, Peter Hoskin wrote:
> Tanner Postert wrote:
> > I don't think it's built in, so I was wondering ya'll would recommend as
> > the best way to convert int to string, not basic type casting, but
> > converting to the english word the int represents.
> >
> > Something like this:
> >
> > 5 = "Five" or 20 = "Twenty"
>
> You're right - its not built in.
>
> You could use something like this
>
> function numerictotext($numeric) {
>   for ($i;$i < strlen($numeric);$i++) {
>     $character = substr($numeric,$i,1);
>     switch ($character) {
>       case 0:
>         $return .= 'Zero ';
>         break;
>       case 1:
>         $return .= 'One ';
>         break;
>     }
>   }
>   return $return;
> }
>
> Regards,
> Peter Hoskin
> Do I get a cookie?

Actually, this isn't far off.  I would think that you would have to treat the 
numbers in the base 10 thinking of it.  (I realized this because my wife 
asked me how binary worked last night, and it was fresh in my head of trying 
to explain how base 10 actually works)

This would mean find out how many characters are in the string (yes, make sure 
it is a string, not int).  Let's assume the number is 256.  

So then you know that you are using 3 placeholders, btw, this would only work 
for int... not float.

So, for each place holder you have a maximum of 10 possibilities.

2 - hundred
5 - ten
6 - one

for the hundreds place you would have a nice big switch statement
1 = "one hundred"
2 = "two hundred"
etc.

for the tens place you have another switch statement
1 = "I'm not sure how 1 would work just yet, but it's not far off"
2 = 'twenty'
3 = 'thirty'

and for the Ones place another switch statement
1= "One"
2 = "Two"

I think the Ones place would be special depending on if the tens place is a 1

I hope I explained it good enough... working under a deadline at work ;)

-- 
Ray Hauge
Programmer/Systems Administrator
American Student Loan Services
www.americanstudentloan.com
1.800.575.1099

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux