All pastes #389161 Raw Edit

Stuff

public text v1 · immutable
·published 2007-03-10 15:28 UTC
rendered paste body
<?php

function makewords($numval) {
$moneystr = "";
// Millions
$milval = (integer)($numval / 1000000);
if($milval == 1) {
  $moneystr .= "One million";
  }
if($milval > 1) {
  $moneystr .= getwords($milval) . " million";
  }
// thousands
$workval = $numval - ($milval * 1000000); // get rid of millions
$thouval = (integer)($workval / 1000);
if($thouval > 0) {
  $workword = getwords($thouval);
  if ($moneystr == "") {
    $moneystr = $workword . " thousand";
    } else {
    $moneystr .= " " . $workword . " thousand";
    }
  }
// handle all the rest of the money
$workval = $workval - ($thouval * 1000); // get rid of thousands
$tensval = (integer)($workval);
if ($moneystr == 0) {
  if ($tensval > 0) {
    $moneystr .= getwords($tensval);
    } else {
    $moneystr .= "Zero";
    }
  } else {
// non zero values in hundreds and up
  $workword = getwords($tensval);
  $moneystr .= " " . $workword;
  }
// plural or singular
$workval = (integer)($numval);
// do the pennies - use printf so that we get the
// same rounding as printf
// done - let's get out of here!
return $moneystr;
}

//*************************************************************
// this function creates word phrases in the range of 1 to 999.
// pass it an integer value
//*************************************************************
function getwords($workval) {
$numwords = array(
    1 => "one",
    2 => "two",
    3 => "three",
    4 => "four",
    5 => "five",
    6 => "six",
    7 => "seven",
    8 => "eight",
    9 => "nine",
    10 => "ten",
    11 => "eleven",
    12 => "twelve",
    13 => "thirteen",
    14 => "fourteen",
    15 => "fifteen",
    16 => "sixteen",
    17 => "seventeen",
    18 => "eighteen",
    19 => "nineteen",
    20 => "twenty",
    30 => "thirty",
    40 => "forty",
    50 => "fifty",
    60 => "sixty",
    70 => "seventy",
    80 => "eighty",
    90 => "ninety");


    // handle the 100's
    $retstr = " ";
    $hundval = (integer)($workval / 100);
    if($hundval)
    $retstr .= $numwords[$hundval]." hundred";

    // handle units and teens
    $workstr = "";
    $tensval = $workval - ($hundval * 100); // dump the 100's
    $tempval = ((integer)($tensval / 10)) * 10;
    $unitval = $tensval - $tempval;
    if (($tensval < 20) && ($tensval > 0))
    {// do the teens
        $workstr = $numwords[$tensval];
    }
    else
    {// got to break out the units and tens
        $workstr = $numwords[$tempval]; // get the tens
        if ($unitval > 0)
        {
            $workstr .= "-" . $numwords[$unitval];
        }
    }
    // join all the parts together and leave
    if ($workstr != "")
    {
        if ($retstr != "")
        {
            $retstr .= " " . $workstr;
        }
        else
        {
            $retstr = $workstr;
        }
    } 
    return $retstr;
}
?>