function convertNum($num)
{
    $num = (int) $num;
    // make sure it's an integer
    if ($num < 0) {
        return "negative" . convertTri(-$num, 0);
    }
    if ($num == 0) {
        return "zero";
    }
    return convertTri($num, 0);
}
Esempio n. 2
0
function numtowords($num, $doth = false, $addcontractiontonum = false)
{
    global $placevals;
    if ($addcontractiontonum) {
        $num = strval($num);
        $len = strlen($num);
        $last = $num[$len - 1];
        if ($len > 1 && $num[$len - 2] == "1") {
            //ie 612
            $c = "th";
        } else {
            if ($last == "1") {
                $c = "st";
            } else {
                if ($last == "2") {
                    $c = "nd";
                } else {
                    if ($last == "3") {
                        $c = "rd";
                    } else {
                        $c = "th";
                    }
                }
            }
        }
        return $num . $c;
    }
    if ($num == 0) {
        return "zero";
    }
    $int = floor($num);
    $dec = $num - $int;
    $out = '';
    if ($int > 0) {
        $out .= convertTri($int, 0, $doth);
        if ($dec > 0) {
            $out .= " and ";
        }
    }
    if ($dec > 0) {
        $cnt = 0;
        while (abs($dec - round($dec)) > 1.0E-9 && $cnt < 9) {
            $dec *= 10;
            $cnt++;
        }
        $out .= convertTri(round($dec), 0);
        $out .= ' ' . $placevals[$cnt];
        if ($dec != 1) {
            $out .= 's';
        }
    }
    return trim($out);
}