Skip to content

Commit

Permalink
modify file and make it object oriented
Browse files Browse the repository at this point in the history
  • Loading branch information
nfraz007 committed May 19, 2017
1 parent 2466ec7 commit ce27179
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 135 deletions.
232 changes: 119 additions & 113 deletions NumberToWord.php
Original file line number Diff line number Diff line change
@@ -1,132 +1,138 @@
<?php

$SPACE = ' ';
$DASH = '-';
class NumberToWord{

$UNIT = array(
3 => 'thousand',
6 => 'million',
9 => 'billion',
12 => 'trillion',
15 => 'quadrillion',
18 => 'quintillion',
21 => 'sextillion',
24 => 'septillion',
27 => 'octillion',
30 => 'nonillion',
33 => 'decillion',
36 => 'undecillion',
39 => 'duodecillion',
42 => 'tredecillion',
45 => 'quattuordecillion',
48 => 'quindecillion',
51 => 'sexdecillion',
54 => 'septendecillion',
57 => 'octadecillion',
60 => 'novemdecillion',
63 => 'vigintillion',
66 => 'unvigintillion',
69 => 'duovigintillion',
72 => 'trevigintillion',
75 => 'quattuorvigintillion',
78 => 'quinvigintillion',
81 => 'sexvigintillion',
84 => 'septenvigintillion',
87 => 'octavigintillion',
90 => 'novemvigintillion',
93 => 'trigintillion',
96 => 'untrigintillion',
99 => 'duotrigintillion',
);
private $SPACE = ' ';
private $DASH = '-';
private $UNIT = array();
private $WORD = array();

$WORD = array(
0 => 'zero',
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 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred'
);
function __construct(){
$this->init();
}

private function init(){
$this->UNIT = array(
3 => 'thousand',
6 => 'million',
9 => 'billion',
12 => 'trillion',
15 => 'quadrillion',
18 => 'quintillion',
21 => 'sextillion',
24 => 'septillion',
27 => 'octillion',
30 => 'nonillion',
33 => 'decillion',
36 => 'undecillion',
39 => 'duodecillion',
42 => 'tredecillion',
45 => 'quattuordecillion',
48 => 'quindecillion',
51 => 'sexdecillion',
54 => 'septendecillion',
57 => 'octadecillion',
60 => 'novemdecillion',
63 => 'vigintillion',
66 => 'unvigintillion',
69 => 'duovigintillion',
72 => 'trevigintillion',
75 => 'quattuorvigintillion',
78 => 'quinvigintillion',
81 => 'sexvigintillion',
84 => 'septenvigintillion',
87 => 'octavigintillion',
90 => 'novemvigintillion',
93 => 'trigintillion',
96 => 'untrigintillion',
99 => 'duotrigintillion',
);

function convert($number){
global $SPACE,$DASH,$UNIT;
$this->WORD = array(
0 => 'zero',
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 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred'
);
}

$output="";
public function convert($number){
$output="";

if(is_numeric($number)){
$number=str_replace('+', '', $number);
$number=str_replace('-', '', $number);
$number=str_replace('.', '', $number);
$len=strlen($number);
if(is_numeric($number)){
$number=str_replace('+', '', $number);
$number=str_replace('-', '', $number);
$number=str_replace('.', '', $number);
$len=strlen($number);

if($len<=99){
for($i=$len;$i>0;$i-=3){
$pos=3*((int)($len/3)-(int)($i/3));
$start=$i-3;
$stop=3;
if($start<0){
$stop=3+$start;
$start=0;
if($len<=99){
for($i=$len;$i>0;$i-=3){
$pos=3*((int)($len/3)-(int)($i/3));
$start=$i-3;
$stop=3;
if($start<0){
$stop=3+$start;
$start=0;
}
$temp=$this->loop(substr($number,$start,$stop)).$this->SPACE;
if($i!=$len) $temp.=$this->UNIT[$pos].$this->SPACE;
$output=$temp.$output;
}
$temp=loop(substr($number,$start,$stop)).$SPACE;
if($i!=$len) $temp.=$UNIT[$pos].$SPACE;
$output=$temp.$output;
return $output;
}else{
throw new Exception("too big number, maximum string length is 99", 1);
}
$output='{"status":"success", "input":"'.$number.'", "output":"'.$output.'"}';
}else{
$output='{"status":"error", "input":"'.$number.'", "output":"too big number"}';
throw new Exception("not a valid number", 1);
}
}else{
$output='{"status":"error", "input":"'.$number.'", "output":"not a valid number"}';
}
return $output;
}

function loop($number){
global $SPACE,$DASH,$WORD;
private function loop($number){
$number=(int)$number;
$output="";

$number=(int)$number;
$output="";

if($number<=20){
$output=$WORD[$number];
}elseif($number<=99){
$base=10;
$main=$number/$base;
$rest=$number%$base;
$output=$WORD[(int)$main*$base];
if($rest) $output.=$DASH.$WORD[$rest];
}else{
$base=100;
$main=$number/$base;
$rest=$number%$base;
$output=loop((int)$main).$SPACE.$WORD[$base];
if($rest) $output.=$SPACE.loop($rest);
if($number<=20){
$output=$this->WORD[$number];
}elseif($number<=99){
$base=10;
$main=$number/$base;
$rest=$number%$base;
$output=$this->WORD[(int)$main*$base];
if($rest) $output.=$this->DASH.$this->WORD[$rest];
}else{
$base=100;
$main=$number/$base;
$rest=$number%$base;
$output=$this->loop((int)$main).$this->SPACE.$this->WORD[$base];
if($rest) $output.=$this->SPACE.$this->loop($rest);
}
return $output;
}
return $output;
}

?>
35 changes: 15 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,30 @@ A php file to convert any number to word form.
# Documentation
You can find the documentation at https://nfraz007.github.io/NumberToWord/

## Setup
# Setup

First, import the file NumberToWord.php
Setup is very easy, just import the file into your project and you are all set.

```
require 'NumberToWord.php';
include("NumberToWord.php");
```

### Function name and parameter
To convert any number to its word form, you have to call a function convert(), which take one parameter as string and give a output in json. Please see the function detail given below.
or

name : convert()
```
require 'NumberToWord.php';
```

input : string
# Convert a number
To convert any number to its word form, create an instance of NumberToWord class. you can then call the convert() function on the NumberToWord object, passing one parameter as number you want to convert

output : json
* Input must be a integer value in string format.

### Maximum limit of length of input string = 99
* **Maximum limit of length of input string = 99**

### Convert number
```
// call convert(string) funtion, it will return a json string.
$output = convert('123');
$output = convert('1a');
$output = convert('0');
/*
{"status":"success", "input":"123", "output":"one hundred twenty-three"}
{"status":"error", "input":"1a", "output":"not a valid number"}
{"status":"success", "input":"0", "output":"zero"}
*/
$obj = new NumberToWord();
echo $obj->convert("123");
```

> one hundred twenty-three
6 changes: 4 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
require_once 'NumberToWord.php';

$number=123;
echo convert($number);
$number='123';
$obj=new NumberToWord();

echo $obj->convert($number);
?>

0 comments on commit ce27179

Please sign in to comment.