Jump to content

class conversion number to word


ekowk

Recommended Posts

Dear All PHP Master,
Kindly need your help (I'm sorry my Bad English), I'm new in PHP and OOP in PHP please help to check I have class to convert number to words below, but when I'm running it the result in browser is error with error message :
  Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 16
  Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 14
  Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 16
  seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan

here it's my code to convert number to words :
<?php 
   Class Terbilang {

      Public function terbilang() {
         $this->dasar = array(1=>'satu','dua','tiga','empat','lima','enam','tujuh','delapan','sembilan');
         $this->angka = array(1000000000000,1000000000,1000000,1000,100,10,1);
         $this->satuan = array('triliun','milyar','juta','ribu','ratus','puluh','');
      }
      
      Public function eja($n) {
         $i=0;
         while($n!=0){
            $count = (int)($n/$this->angka[$i]);
            if($count>=10) $str .= $this->eja($count). " ".$this->satuan[$i]." ";
            else if($count > 0 && $count < 10)
            $str .= $this->dasar[$count] . " ".$this->satuan[$i]." ";
            $n -= $this->angka[$i] * $count;
            $i++;
         }
         $str = preg_replace("/satu puluh (\w+)/i","\\1 belas",$str);
         $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i","se\\1",$str);
         return $str;
      }
   }
   
   $bilangan = new Terbilang;
   echo $bilangan -> eja(123456789);   //shouldbe show :seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.