blog
PHP’de Sayıları Yazıya Nasıl Çeviririz?

PHP’de Sayıları Yazıya Nasıl Çeviririz?

Merhaba Arkadaşlar, Bu eğitim tamamen PHP’de Sayıları Kelimelere Dönüştürme ile ilgilidir. PHP’de Sayıları Kelimelere Nasıl Çevireceğinizi öğreneceğiniz için bu eğitim çok kolay ve yararlıdır . O halde başlayalım:

Birincisi, “index.php” adında bir sınıf oluşturmak ve şu kodu koymaktır:

KOD
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Numarayı Yazıya Çevirme</title>
</head>

<body>
<form method="post" action="">
  <label>Sayı Giriniz
  <input type="text" name="textfield" onchange="convert_number_to_words()" />
  </label>
  <input type="submit" value="Çevir" />
</form>
<p>
  Equivalent  in words: 
  <?php
function convert_number_to_words($number) {
   
    $hyphen      = '-';
    $conjunction = '  ';
    $separator   = ' ';
    $negative    = 'eksi ';
    $decimal     = ' nokta ';
    $dictionary  = array(
        0                   => 'Sıfır',
        1                   => 'Bir',
        2                   => 'İki',
        3                   => 'Üç',
        4                   => 'Dört',
        5                   => 'Beş',
        6                   => 'Altı',
        7                   => 'Yedi',
        8                   => 'Sekiz',
        9                   => 'Dokuz',
        10                  => 'On',
        11                  => 'OnBir',
        12                  => 'Onİki',
        13                  => 'OnÜç',
        14                  => 'OnDört',
        15                  => 'OnBeş',
        16                  => 'OnAltı',
        17                  => 'OnYedi',
        18                  => 'OnSekiz',
        19                  => 'OnDokuz',
        20                  => 'Yirmi',
        30                  => 'Otuz',
        40                  => 'Kırk',
        50                  => 'Elli',
        60                  => 'Atmış',
        70                  => 'Yetmiş',
        80                  => 'Seksen',
        90                  => 'Doksan',
        100                 => 'Yüz',
        1000                => 'Bin',
        1000000             => 'Milyon',
        1000000000          => 'Milyar',
        1000000000000       => 'Trilyon',
        1000000000000000    => 'Katrilyon',
        1000000000000000000 => 'Kentrilyon'
    );
   
    if (!is_numeric($number)) {
        return false;
    }
   
    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }
   
    $string = $fraction = null;
   
    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }
   
    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }
   
    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }
   
    return $string;
}

echo '<b>'.convert_number_to_words($_POST['textfield']).'</b>';
?>
</p>
</body>
</html>

Bu kadar basit. Git programı test et!

Erkan Çağlar

2002'den beri yazılım geliştiriyorum. İşletmelere özel yönetim paneli, ödeme sistemleri ve web projeleri yapıyorum.

erkancaglar.com'da hizmetlerim →

Yorumlar (3)

Ö
önemli8 Kasım 2023
çalışmıyor kral
E
erkan15 Ocak 2024
yapamadığınız yada hata aldıpğınız yeri paylaşırsanız yardımcı olmak isterim.
Ö
Önemli Değil8 Kasım 2023
http://www.w3schools.com/php/phptryit.asp?filename=tryphp_compiler

bu siteden denedim sonuç şu:

Sayı Giriniz [123123] Çevir

Equivalent in words:

Yorum yazın

Yanıtlanan:
Yorumlar onaylandıktan sonra yayınlanır. E-posta adresiniz kimseyle paylaşılmaz.