Easy (I think)Script Modification help needed.

I already have a complete script which takes a decimal number and converts it to words. (Found it online after extensive search. Free for use to others.)

Example use of existing script:
48.95 is converted to Forty-eight Dollars and Ninety-five Cents.

I need to have the script modified to write the words as follows:

Forty-eight and 95/100 Dollars.

When the decimal number has zeros after the decimal the conversion should be like this:
48.00 converts to Forty-eight and 00/100 Dollars. (Currently the script just writes: 48 Dollars for this number.)

This would need to be for any decimal number as limited by the existing script.

Can anyone give me some direction on what to edit to make this happen?

Here is the existing code:

[code] <?php

/** Serialized Array of big names, thousand, million, etc

  • @package NumberToText /
    define(“N2T_BIG”, serialize(array(‘thousand’, ‘million’, ‘billion’, ‘trillion’, ‘quadrillion’, ‘quintillion’, ‘sextillion’, ‘septillion’, ‘octillion’, ‘nonillion’, ‘decillion’, ‘undecillion’, ‘duodecillion’, ‘tredecillion’, ‘quattuordecillion’, ‘quindecillion’, ‘sexdecillion’, ‘septendecillion’, ‘octodecillion’, ‘novemdecillion’, ‘vigintillion’, ‘unvigintillion’, ‘duovigintillion’, ‘trevigintillion’, ‘quattuorvigintillion’, ‘quinvigintillion’, ‘sexvigintillion’, ‘septenvigintillion’, ‘octovigintillion’, ‘novemvigintillion’, ‘trigintillion’, ‘untrigintillion’, ‘duotrigintillion’, ‘tretrigintillion’, ‘quattuortrigintillion’, ‘quintrigintillion’, ‘sextrigintillion’, ‘septentrigintillion’, ‘octotrigintillion’, ‘novemtrigintillion’)));
    /
    * Serialized Array of medium names, twenty, thirty, etc
  • @package NumberToText /
    define(“N2T_MEDIUM”, serialize(array(2=>‘twenty’, 3=>‘thirty’, 4=>‘fourty’, 5=>‘fifty’, 6=>‘sixty’, 7=>‘seventy’, 8=>‘eighty’, 9=>‘ninety’)));
    /
    * Serialized Array of small names, zero, one, etc… up to eighteen, nineteen
  • @package NumberToText /
    define(“N2T_SMALL”, serialize(array(‘zero’, ‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’, ‘ten’, ‘eleven’, ‘twelve’, ‘thirteen’, ‘fourteen’, ‘fifteen’, ‘sixteen’, ‘seventeen’, ‘eighteen’, ‘nineteen’)));
    /
    * Word for “dollars”
  • @package NumberToText /
    define(“N2T_DOLLARS”, “dollars”);
    /
    * Word for one “dollar”
  • @package NumberToText /
    define(“N2T_DOLLARS_ONE”, “dollar”);
    /
    * Word for “cents”
  • @package NumberToText /
    define(“N2T_CENTS”, “cents”);
    /
    * Word for one “cent”
  • @package NumberToText /
    define(“N2T_CENTS_ONE”, “cent”);
    /
    * Word for “and”
  • @package NumberToText /
    define(“N2T_AND”, “and”);
    /
    * Word for “negative”
  • @package NumberToText */
    define(“N2T_NEGATIVE”, “negative”);

/** Number to text converter. Converts a number into a textual description, such as

  • “one hundred thousand and twenty-five”.
  • Now supports any size number, and negative numbers. To pass numbers > 2 ^32, you must
  • pass them as a string, as PHP only has 32-bit integers.
  • @param int $number The number to convert
  • @param bool $currency True to convert as a dollar amount
  • @param bool $capatalize True to capatalize every word (except “and”)
  • @param bool $and True to use “and” (ie. “one hundred AND six”)
  • @return The textual description of the number, as a string.
  • @package NumberToText
    */

function NumberToText($number, $currency = false, $capatalize = false, $and = true) {
$big = unserialize(N2T_BIG);
$small = unserialize(N2T_SMALL);

// get rid of leading 0's
/*
while ($number{0} == 0) {
	$number = substr($number,1);
}
*/

$text = "";

//$negative = ($number < 0); // check for negative
//$number = abs($number); // make sure we have a +ve number
if (substr($number, 0, 1) == "-") {
	$negative = true;
	$number = substr($number,1); // abs()
} else {
	$negative = false;
}

// get the integer and decimal parts
//$int_o = $int = floor($number); // store into two vars
if ($pos = strpos($number,".")) {
	$int_o = $int = substr($number,0,$pos);
	$decimal_o = $decimal = substr($number,$pos + 1);
} else {
	$int_o = $int = $number;
	$decimal_o = $decimal = 0;
}
// $int_o and $decimal_o are for "original value"

// conversion for integer part:

$section = 0; // $section controls "thousand" "million" etc
do {
	// keep breaking down into 3 digits ($convert) and the rest
	//$convert = $int % 1000;
	//$int = floor($int / 1000);
	
	if ($section > count($big) - 1) {
		// ran out of names for numbers this big, call recursively
		$text = NumberToText($int, false, false, $and)." ".$big[$section-1]." ".$text;
		$int = 0;
	} else { 
		// we can handle it
		
		$convert = substr($int, -3); // grab the last 3 digits
		$int = substr($int, 0, -1 * strlen($convert));
		
		if ($convert > 0) {
			// we have something here, put it in
			$text = trim(n2t_convertthree($convert, $and, ($int > 0)).(isset($big[$section-1]) ? ' '.$big[$section-1].' ' : '').$text);
		}
	}
	
	$section++;
} while ($int > 0);


// conversion for decimal part:

if ($currency && floor($number)) {
	// add " dollars"
	$text .= " ".($int_o == 1 ? N2T_DOLLARS_ONE : N2T_DOLLARS)." ";
}

if ($decimal && $currency) {
	// if we have any cents, add those
	if ($int_o > 0) {
		$text .= " ".N2T_AND." ";
	}
	
	$cents = substr($decimal,0,2); // (0.)2342 -> 23
	$decimal = substr($decimal,2); // (0.)2345.. -> 45..
	
	$text .= n2t_convertthree($cents, false, true); // explicitly show "and" if there was an $int
}

if ($decimal) {
	// any remaining decimals (whether or not $currency is set)
	$text .= " point";
	for ($i = 0; $i < strlen($decimal); $i++) {
		// go through one number at a time
		$text .= " ".$small[$decimal{$i}];
	}
}

if ($decimal_o && $currency) {
	// add " cents" (if we're doing currency and had decimals)
	$text .= " ".($decimal_o == 1 ? N2T_CENTS_ONE : N2T_CENTS);
}

// check for negative
if ($negative) {
	$text = N2T_NEGATIVE." ".$text;
}

// capatalize words
if ($capatalize) {
	// easier to capatalize all words then un-capatalize "and"
	$text = str_replace(ucwords(N2T_AND), N2T_AND, ucwords($text));
}

return trim($text);

}

/** This is a utility function of n2t. It converts a 3-digit number

  • into a textual description. Normally this is not called by itself.

  • @param int $number The 3-digit number to convert (0 - 999)

  • @param bool $and True to put the “and” in the string

  • @param bool $preceding True if there are preceding members, puts an

  •                      explicit and in (ie 1001 => one thousand AND one)
    
  • @return The textual description of the number, as a string

  • @package NumberToText
    */
    function n2t_convertthree($number, $and, $preceding) {
    $small = unserialize(N2T_SMALL);
    $medium = unserialize(N2T_MEDIUM);

    $text = “”;

    if ($hundreds = floor($number / 100)) {
    // we have 100’s place
    $text .= $small[$hundreds]." hundred ";
    }
    $tens = $number % 100;
    if ($tens) {
    // we still have values
    if ($and && ($hundreds || $preceding)) {
    $text .= " “.N2T_AND.” ";
    }

     if ($tens < 20) {
     	$text .= $small[$tens];
     } else {
     	$text .= $medium[floor($tens/10)];
     	if ($ones = $tens % 10) {
     		$text .= "-".$small[$ones];
     	}
     }
    

    }

    return $text;
    }

function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$currency = isset($_GET[“currency”]) ? (bool)$_GET[“currency”] : false;
$capatalize = isset($_GET[“capatalize”]) ? (bool)$_GET[“capatalize”] : false;
$and = isset($_GET[“and”]) ? (bool)$_GET[“and”] : !isset($_GET[“num”]); // set to true by default if we didnt submit anything

echo “<form method=“GET”>”;
echo “<input type=text name=“num” value=”".$_GET[“num”]."">";
echo “<input type=submit value=“Convert”>”;
echo “
<input type=checkbox name=“currency” value=“1” “.($currency ? “CHECKED” :””)."> Currency";
echo “
<input type=checkbox name=“capatalize” value=“1” “.($capatalize ? “CHECKED” :””)."> Capatalize";
echo “
<input type=checkbox name=“and” value=“1” “.($and ? “CHECKED” :””)."> Use ‘and’";
echo “”;

if (isset($_GET[‘num’])) {
$num = preg_replace(’/[^0-9.]/’,’’,$_GET[‘num’]);
echo $num . " => ".numbertotext($num, $currency, $capatalize, $and);
echo “



”;
exit;
}

$arr = array(1,2,10,11,12,20,21,100,200,202,220,223,1000,1100,1232,10000,100000,1100000,1000001,1.1,1.23,1.424,1.3005,0.1,0.23,23445.34,349.35,3476233.52);
echo “Normal:

”;

$time_start = getmicrotime();
foreach ($arr as $val) {
//echo number_format($val,6)." => “.numbertotext($val,false,false).”
";
echo $val." => “”.numbertotext($val,false,false).""
";
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo “Completed in $time”;

echo “


Currency:

”;

$time_start = getmicrotime();
foreach ($arr as $val) {
//echo number_format($val,6)." => “.numbertotext($val,true,true).”
";
echo $val." => “.numbertotext($val,true,true).”
";
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo “Completed in $time”;

echo “


”;

$arr = array(-1,-32754264,7823572834.23432,“842673284525.3253”,-69,1000000000,10000000000,100000000000,“1000000000000”,“10000000000000”,“100000000000000”,“1000000000000000”,“10000000000000000”,“100000000000000000”,“1000000000000000000”,“1000000000000000000000”,“1000000000000000000000000”,“1000000000000000000000000000”,“1000000000000000000000000000000”,“1000000000000000000000000000000000”,“1000000000000000000000000000000000000”,“1000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000000000000000000”,“1000000000000000000000000000000000000000000000000000000000000000000”,“1735493936528346132019285410348712730057341562931024518346213403490”,"-8365102934619034174538361493275960273462592739257246349237426342384.38651",“100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”);
echo “Normal:

”;

$time_start = getmicrotime();
foreach ($arr as $val) {
//echo number_format($val,6)." => “.numbertotext($val,false,false).”
";
echo $val." => “.numbertotext($val,false,false).”
";
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo “Completed in $time”;

echo “


Currency:

”;

$time_start = getmicrotime();
foreach ($arr as $val) {
//echo number_format($val,6)." => “.numbertotext($val,true,true).”
";
echo $val." => “.numbertotext($val,true,true).”
";
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo “Completed in $time”;

?>[/code]

Realizing that you did not write that code. Read through it and see how it works. How do you think it knows there are numbers behind the decimal?

How does it convert them to alpha characters and when?

Should be somewhat easy to spot how to stop if from changing the cents into letters and instead just use the numbers.

Yep! That is precisely what I did :slight_smile: It was good to get back into it :slight_smile:
Thanks!

I wanted to check if there might be anyone that could help me? Is there something I might be doing wrong? I know it’s not coded nicely and I apologize for this. Any help would be much appreciated.

  1. Start a new topic if yu have a question.

  2. I don’t see a question or anything actually pertaining to a question here.

Sponsor our Newsletter | Privacy Policy | Terms of Service