| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::THA::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Thai | ||
| 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 | # {{{ num2tha_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2tha_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 @units = qw(ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า); | ||
| 36 | |||
| 37 | return $units[$positive] if ($positive >= 0 && $positive <= 9); | ||
| 38 | return 'สิบ' if ($positive == 10); | ||
| 39 | |||
| 40 | my $out = ''; | ||
| 41 | my $remain; | ||
| 42 | |||
| 43 | # Helper: append remainder, substituting เอ็ด for bare หนึ่ง in compounds. | ||
| 44 | # In Thai, the digit 1 in the units position of any compound number | ||
| 45 | # is always เอ็ด (et), never หนึ่ง (nueng). | ||
| 46 | my $append_remain = sub { | ||
| 47 | my ($so_far, $r) = @_; | ||
| 48 | if ($r == 1) { | ||
| 49 | return $so_far . 'เอ็ด'; | ||
| 50 | } | ||
| 51 | return $so_far . num2tha_cardinal($r); | ||
| 52 | }; | ||
| 53 | |||
| 54 | if ($positive > 10 && $positive < 20) { # 11 .. 19 | ||
| 55 | $remain = $positive % 10; | ||
| 56 | $out = 'สิบ'; | ||
| 57 | $out .= $remain == 1 ? 'เอ็ด' : $units[$remain]; | ||
| 58 | } | ||
| 59 | elsif ($positive >= 20 && $positive < 100) { # 20 .. 99 | ||
| 60 | my $tens = int($positive / 10); | ||
| 61 | $remain = $positive % 10; | ||
| 62 | |||
| 63 | $out = $tens == 2 ? 'ยี่' : $units[$tens]; | ||
| 64 | $out .= 'สิบ'; | ||
| 65 | if ($remain) { | ||
| 66 | $out .= $remain == 1 ? 'เอ็ด' : $units[$remain]; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | elsif ($positive >= 100 && $positive < 1000) { # 100 .. 999 | ||
| 70 | my $hundreds = int($positive / 100); | ||
| 71 | $remain = $positive % 100; | ||
| 72 | |||
| 73 | $out = $units[$hundreds] . 'ร้อย'; | ||
| 74 | $out = $append_remain->($out, $remain) if $remain; | ||
| 75 | } | ||
| 76 | elsif ($positive >= 1000 && $positive < 10_000) { # 1_000 .. 9_999 | ||
| 77 | my $thousands = int($positive / 1000); | ||
| 78 | $remain = $positive % 1000; | ||
| 79 | |||
| 80 | $out = $units[$thousands] . 'พัน'; | ||
| 81 | $out = $append_remain->($out, $remain) if $remain; | ||
| 82 | } | ||
| 83 | elsif ($positive >= 10_000 && $positive < 100_000) { # 10_000 .. 99_999 | ||
| 84 | my $ten_thousands = int($positive / 10000); | ||
| 85 | $remain = $positive % 10000; | ||
| 86 | |||
| 87 | $out = $units[$ten_thousands] . 'หมื่น'; | ||
| 88 | $out = $append_remain->($out, $remain) if $remain; | ||
| 89 | } | ||
| 90 | elsif ($positive >= 100_000 && $positive < 1_000_000) { # 100_000 .. 999_999 | ||
| 91 | my $hundred_thousands = int($positive / 100000); | ||
| 92 | $remain = $positive % 100000; | ||
| 93 | |||
| 94 | $out = $units[$hundred_thousands] . 'แสน'; | ||
| 95 | $out = $append_remain->($out, $remain) if $remain; | ||
| 96 | } | ||
| 97 | elsif ($positive >= 1_000_000 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999 | ||
| 98 | my $millions = int($positive / 1_000_000); | ||
| 99 | $remain = $positive % 1_000_000; | ||
| 100 | |||
| 101 | $out = num2tha_cardinal($millions) . 'ล้าน'; | ||
| 102 | $out = $append_remain->($out, $remain) if $remain; | ||
| 103 | } | ||
| 104 | |||
| 105 | return $out; | ||
| 106 | } | ||
| 107 | |||
| 108 | # }}} | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ |