PHP.lt konferencija [ php-konf@php.lt ] :: Re: [php-konf] Skaičių vertimas žodžiais


Re: [php-konf] Skaičių vertimas žodžiais From: Justinas (justk@takas.lt)
Date: 2004-07-13 18:03:51


ganetinai primityvus, bet veikia en/lt kalbom. enjoy

function numberToWords ( $digit, $lang, $curr = 'LTL' ) {
                $dparts = explode ( ".", $digit );
                $digit = $dparts[0];
                $ct = $dparts[1];
                $digitLength = (int)strlen( $digit );
                //apsirasome zodziu masyva
                $textOnes['lt'] = array( '', ' vienas', ' du', ' trys', ' keturi', ' penki',
' šeši', ' septyni', ' aštuoni', ' devyni' );
                $textTens['lt'] = array( '', ' dešimt', ' dvidešimt', ' trisdešimt', '
keturiasdešimt', ' penkiasdešimt', ' šešiasdešimt', ' septyniasdešimt', '
astuoniasdešimt', ' devyniasdešimt' );
                $textExepts['lt'] = array( ' vienuolika', ' dvylika', ' trylika', '
keturiolika', ' penkiolika', ' šešiolika', ' septyniolika', ' aštuoniolika',
' devyniolika' );
                $textHundrets['lt'] = array( '', ' šimtas', ' šimtai' );
                $textThousands['lt'] = array( ' tūkstančių', ' tūkstantis', '
tūkstančiai' );

                $textOnes['en'] = array( '', ' one', ' two', ' three', ' four', ' five', '
six', ' seven', ' eight', ' nine' );
                $textTens['en'] = array( '', ' ten', ' twenty', ' thirty', ' fourty', '
fifty', ' sixty', ' seventy', ' eighty', ' ninety' );
                $textExepts['en'] = array( ' eleven', ' twelve', ' thirteen', ' fourteen', '
fifteen', ' sixteen', ' seventeen', ' eighteen', ' nineteen' );
                $textHundrets['en'] = array( '', ' hundred', ' hundred' );
                $textThousands['en'] = array( ' thousand', ' thousand', ' thousand' );
                $lcur['LTL'] = 'USD';
                $lcur['USD'] = 'LTL';
                $words = '';
                                // nu ble ir su neigiamais malames:
                if ( $digit < 0 ) {
                        $words = 'Minus';
                }
                if( $digitLength < 7 ){
                          for( $d = 2; $d > 0; $d-- ){
                      //checaks - tajp rejkia
                      if( ( $d == 2 ) && ( $digitLength < 4 ) ) continue;
                      //gaunam temp skaitmeny
                      $digitLocal = substr( $digit, -3 );
                      if( $d == 2 ) $digitLocal = substr( $digit, 0, $digitLength - 3 );

                      //gaunam rejkiamus skaicius
                      $digitLocalLength = strlen( $digitLocal );
                      $hundrets = substr( $digitLocal, -3, 1 );
                      $lastTwo = substr( $digitLocal, -2 );
                      //cia konstruojam simtus
                      if( $digitLocalLength > 2 ){
                          $words .= $textOnes[$lang][(int)$hundrets];
                          if( $hundrets > 1 ){
                              $words .= $textHundrets[$lang][2];
                          } else {
                              $words .= $textHundrets[$lang][(int)$hundrets];
                          }
                      }
                      //sia jai qrwos nuo 10 iki 20
                      if( ( $lastTwo > 10 ) && ( $lastTwo < 20 ) ){
                          $words .= $textExepts[$lang][(int)substr( $lastTwo, -1 ) - 1];
                          //cia jei rejkia pridedam zody 'tukstantis' pagal linksny
                          if( $d == 2 ) $words .= $textThousands[$lang][0];
                      //cia bet kuriuo kitu atveju
                      } else {
                          //jai yra desimtys
                          if( $digitLocalLength > 1 ) $words .= $textTens[$lang]
[(int)substr( $lastTwo, -2, 1 ) ] . ' ';
                          $words .= $textOnes[$lang][(int)substr( $lastTwo, -1 )];
                          //cia jei rejkia pridedam zody 'tukstantis' pagal linksny
                          if( $d == 2 ){
                              if( ( (int)substr( $lastTwo, -1 ) ) > 1 ){
                                  $words .= $textThousands[$lang][2];
                              } else {
                                  $words .= $textThousands[$lang][(int)substr( $lastTwo,
-1 )];
                              }
                          }
                      }
                  }
                  $words = trim( $words );
                if ( $words[0] . $words[1] == 'š' )
                        $words = 'Š' . substr( $words, 2 );
                else
                        $words = ucfirst( $words );
                }
                if ( $ct < 10 ) $ct.='0';
                return $words . ' ' . $curr . ', ' . substr($ct, 0, 2) . ' ct';
        }

On Tuesday 13 July 2004 09:30, Audrius Karabanovas wrote:
> Gal kas turite pasirašę skaičių vertimo žodžiais skriptuką?
>
> PVZ: 1.300,01 Lt
> Vienas tūkstantis trys šimtai litų 01 ct.
>
> Jei taip gal galėtumėte pasidalinti?
>
> Su pagarba,
> Audrius Karabanovas




© MMI www.php.lt | E-mail: webmaster@php.lt