-1

Basically I have the following string:

1,254.40

And I have to convert it into the following thing:

one * two * five * four * usd * four * zero * cents

How can I do that? So far I've written something like this:

public function priceToString($price)
{
    $output = "";

    $chars = str_split($price);

    foreach($chars as $char)
    {
        if(is_numeric($char))
        {
            $output .= (string)$char . " *";
        }
    }

    return $output;
}

But it doesn't work because it displays simply integers...How can I solve this?

Gowri
  • 1,832
  • 3
  • 29
  • 53
khernik
  • 2,059
  • 2
  • 26
  • 51
  • 1
    There are cetains APIs available to do this (http://bloople.net/num2text/) You may find this post helpful : http://stackoverflow.com/questions/11500088/php-express-number-in-words – Developer Oct 17 '14 at 10:50
  • You have to write code to convert each number into the text equivalent (and the same with the usd and cents). – i alarmed alien Oct 17 '14 at 10:50
  • you should create a mapping array for each number – madz Oct 17 '14 at 10:55

1 Answers1

1

(Didnt check for validity, but something along these lines:

$numericText = array('zero','one','two','three','four','five','six','seven','eight','nine');

$price = '1,254.40';
$chars = str_split($price);
foreach($chars as $char) {
    if(is_numeric($char)) {
        $output .= $numericText[(int)$char] . " *";
    } elseif($char == '.') {
        $output .= 'usd *';
    }
}
$output .= ' cents';
RichardBernards
  • 3,146
  • 1
  • 22
  • 30