| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::TUR::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Turkish | ||
| 5 | |||
| 6 | use 5.16.0; | ||
| 7 | use utf8; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | # {{{ use block | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | use Export::Attrs; | ||
| 14 | use Readonly; | ||
| 15 | |||
| 16 | # }}} | ||
| 17 | # {{{ variable declarations | ||
| 18 | |||
| 19 | my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present'; | ||
| 20 | our $VERSION = '0.2603260'; | ||
| 21 | |||
| 22 | # }}} | ||
| 23 | |||
| 24 | # {{{ num2tur_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2tur_cardinal :Export { | ||
| 27 | my $positive = shift; | ||
| 28 | |||
| 29 | croak 'You should specify a number from interval [0, 999_999_999]' | ||
| 30 | if !defined $positive | ||
| 31 | || $positive !~ m{\A\d+\z}xms | ||
| 32 | || $positive < 0 | ||
| 33 | || $positive > 999_999_999; | ||
| 34 | |||
| 35 | my @ones = qw(sıfır bir iki üç dört beş altı yedi sekiz dokuz); | ||
| 36 | my @tens = qw(on yirmi otuz kırk elli altmış yetmiş seksen doksan); | ||
| 37 | |||
| 38 | return $ones[$positive] if ($positive >= 0 && $positive < 10); # 0 .. 9 | ||
| 39 | return $tens[$positive / 10 - 1] if ($positive >= 10 && $positive < 100 && $positive % 10 == 0); # 10,20,..,90 | ||
| 40 | |||
| 41 | my $out; | ||
| 42 | my $remain; | ||
| 43 | |||
| 44 | if ($positive > 9 && $positive < 100) { # 11 .. 99 | ||
| 45 | my $ten_idx = int($positive / 10); | ||
| 46 | $remain = $positive % 10; | ||
| 47 | |||
| 48 | $out = $tens[$ten_idx - 1] . ' ' . $ones[$remain]; | ||
| 49 | } | ||
| 50 | elsif ($positive == 100) { # 100 | ||
| 51 | $out = 'yüz'; | ||
| 52 | } | ||
| 53 | elsif ($positive > 100 && $positive < 1000) { # 101 .. 999 | ||
| 54 | my $hun_idx = int($positive / 100); | ||
| 55 | $remain = $positive % 100; | ||
| 56 | |||
| 57 | $out = $hun_idx == 1 ? 'yüz' : $ones[$hun_idx] . ' yüz'; | ||
| 58 | $out .= $remain ? ' ' . num2tur_cardinal($remain) : ''; | ||
| 59 | } | ||
| 60 | elsif ($positive >= 1000 && $positive < 1_000_000) { # 1000 .. 999_999 | ||
| 61 | my $tho_idx = int($positive / 1000); | ||
| 62 | $remain = $positive % 1000; | ||
| 63 | |||
| 64 | $out = $tho_idx == 1 ? 'bin' : num2tur_cardinal($tho_idx) . ' bin'; | ||
| 65 | $out .= $remain ? ' ' . num2tur_cardinal($remain) : ''; | ||
| 66 | } | ||
| 67 | elsif ($positive >= 1_000_000 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999 | ||
| 68 | my $mil_idx = int($positive / 1_000_000); | ||
| 69 | $remain = $positive % 1_000_000; | ||
| 70 | |||
| 71 | $out = num2tur_cardinal($mil_idx) . ' milyon'; | ||
| 72 | $out .= $remain ? ' ' . num2tur_cardinal($remain) : ''; | ||
| 73 | } | ||
| 74 | |||
| 75 | return $out; | ||
| 76 | } | ||
| 77 | |||
| 78 | # }}} | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ |