This topic is locked
[SOLVED]

 Convert NUMBER to WORDS

7/4/2010 9:12:42 PM
PHPRunner General questions
romaldus author

The code below demonstrate how to convert NUMBER to WORDS

<?php

/***********************************************

* Script Name : NumToWords *

* Scripted By : Alex Culango *

* Email : itnapster.som@gmail.com *

***********************************************/

function numtowords($num){

$ones = 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"

);

$tens = array(

2 => "twenty",

3 => "thirty",

4 => "forty",

5 => "fifty",

6 => "sixty",

7 => "seventy",

8 => "eighty",

9 => "ninety"

);

$hundreds = array(

"hundred",

"thousand",

"million",

"billion",

"trillion",

"quadrillion"

); //limit t quadrillion

$num = number_format($num,2,".",",");

$num_arr = explode(".",$num);

$wholenum = $num_arr[0];

$decnum = $num_arr[1];

$whole_arr = array_reverse(explode(",",$wholenum));

krsort($whole_arr);

$rettxt = "";

foreach($whole_arr as $key => $i){

if($i < 20){

$rettxt .= $ones[$i];

}elseif($i < 100){

$rettxt .= $tens[substr($i,0,1)];

$rettxt .= " ".$ones[substr($i,1,1)];

}else{

$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];

$rettxt .= " ".$tens[substr($i,1,1)];

$rettxt .= " ".$ones[substr($i,2,1)];

}

if($key > 0){

$rettxt .= " ".$hundreds[$key]." ";

}

}

if($decnum > 0){

$rettxt .= " and ";

if($decnum < 20){

$rettxt .= $ones[$decnum];

}elseif($decnum < 100){

$rettxt .= $tens[substr($decnum,0,1)];

$rettxt .= " ".$ones[substr($decnum,1,1)];

}

}

return $rettxt;

}

echo numtowords("23431999.99");
?>


In this sample, the code will convert 23431999.99 and the result is:
I want to use this code in the [color="#FF0000"]VIEW PAGE and field.
How i configure it in PHPRUNNER events or PHP code snippet?

A
ann 7/5/2010

Hi,
add your numtowords() function to the After application initialized event.

Then use Custom format in the View as settings dialog on the Visual Editor tab.

Here is a sample:

$value=numtowords($data["FieldName"]);



where FieldName is your actual field name.