| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | package Lingua::VIE::Num2Word; | ||
| 3 | # ABSTRACT: Number to word conversion in Vietnamese | ||
| 4 | |||
| 5 | use 5.16.0; | ||
| 6 | use utf8; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | # {{{ use block | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use Export::Attrs; | ||
| 13 | use Readonly; | ||
| 14 | |||
| 15 | # }}} | ||
| 16 | # {{{ variable declarations | ||
| 17 | |||
| 18 | my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present'; | ||
| 19 | our $VERSION = '0.2603260'; | ||
| 20 | |||
| 21 | # }}} | ||
| 22 | |||
| 23 | # {{{ num2vie_cardinal convert number to text | ||
| 24 | |||
| 25 | sub num2vie_cardinal :Export { | ||
| 26 | my $positive = shift; | ||
| 27 | |||
| 28 | croak 'You should specify a number from interval [0, 999_999_999]' | ||
| 29 | if !defined $positive | ||
| 30 | || $positive !~ m{\A\d+\z}xms | ||
| 31 | || $positive < 0 | ||
| 32 | || $positive > 999_999_999; | ||
| 33 | |||
| 34 | my @units = qw(không một hai ba bốn năm sáu bảy tám chín); | ||
| 35 | |||
| 36 | return $units[$positive] if ($positive >= 0 && $positive < 10); # 0 .. 9 | ||
| 37 | return 'mười' if ($positive == 10); # 10 | ||
| 38 | |||
| 39 | # 11 .. 19 | ||
| 40 | if ($positive > 10 && $positive < 20) { | ||
| 41 | my $unit = $positive % 10; | ||
| 42 | return 'mười' | ||
| 43 | . ($unit == 5 ? ' lăm' | ||
| 44 | : $unit == 4 ? ' bốn' | ||
| 45 | : ' ' . $units[$unit]); | ||
| 46 | } | ||
| 47 | |||
| 48 | my $out; | ||
| 49 | my $remain; | ||
| 50 | |||
| 51 | # 20 .. 99 | ||
| 52 | if ($positive > 19 && $positive < 100) { | ||
| 53 | my $tens = int($positive / 10); | ||
| 54 | $remain = $positive % 10; | ||
| 55 | |||
| 56 | $out = $units[$tens] . ' mươi'; | ||
| 57 | if ($remain == 0) { | ||
| 58 | # nothing | ||
| 59 | } | ||
| 60 | elsif ($remain == 1) { | ||
| 61 | $out .= ' mốt'; | ||
| 62 | } | ||
| 63 | elsif ($remain == 5) { | ||
| 64 | $out .= ' lăm'; | ||
| 65 | } | ||
| 66 | elsif ($remain == 4) { | ||
| 67 | $out .= ' tư'; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $out .= ' ' . $units[$remain]; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | # 100 .. 999 | ||
| 74 | elsif ($positive > 99 && $positive < 1000) { | ||
| 75 | my $hundreds = int($positive / 100); | ||
| 76 | $remain = $positive % 100; | ||
| 77 | |||
| 78 | $out = $units[$hundreds] . ' trăm'; | ||
| 79 | if ($remain > 0 && $remain < 10) { | ||
| 80 | $out .= ' lẻ ' . $units[$remain]; | ||
| 81 | } | ||
| 82 | elsif ($remain >= 10) { | ||
| 83 | $out .= ' ' . num2vie_cardinal($remain); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | # 1_000 .. 999_999 | ||
| 87 | elsif ($positive > 999 && $positive < 1_000_000) { | ||
| 88 | my $thousands = int($positive / 1000); | ||
| 89 | $remain = $positive % 1000; | ||
| 90 | |||
| 91 | $out = num2vie_cardinal($thousands) . ' nghìn'; | ||
| 92 | if ($remain > 0 && $remain < 10) { | ||
| 93 | $out .= ' không trăm lẻ ' . $units[$remain]; | ||
| 94 | } | ||
| 95 | elsif ($remain >= 10 && $remain < 100) { | ||
| 96 | $out .= ' không trăm ' . num2vie_cardinal($remain); | ||
| 97 | } | ||
| 98 | elsif ($remain >= 100) { | ||
| 99 | $out .= ' ' . num2vie_cardinal($remain); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | # 1_000_000 .. 999_999_999 | ||
| 103 | elsif ($positive > 999_999 && $positive < 1_000_000_000) { | ||
| 104 | my $millions = int($positive / 1_000_000); | ||
| 105 | $remain = $positive % 1_000_000; | ||
| 106 | |||
| 107 | $out = num2vie_cardinal($millions) . ' triệu'; | ||
| 108 | if ($remain > 0 && $remain < 1000) { | ||
| 109 | # e.g. 1_000_005 -> một triệu không nghìn không trăm lẻ năm | ||
| 110 | my $sub_hundreds = $remain % 100; | ||
| 111 | my $sub_tens = int($remain / 100); | ||
| 112 | $out .= ' không nghìn'; | ||
| 113 | if ($sub_tens == 0 && $sub_hundreds > 0 && $sub_hundreds < 10) { | ||
| 114 | $out .= ' không trăm lẻ ' . $units[$sub_hundreds]; | ||
| 115 | } | ||
| 116 | elsif ($sub_tens == 0 && $sub_hundreds >= 10) { | ||
| 117 | $out .= ' không trăm ' . num2vie_cardinal($sub_hundreds); | ||
| 118 | } | ||
| 119 | elsif ($sub_tens > 0) { | ||
| 120 | $out .= ' ' . num2vie_cardinal($remain); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | elsif ($remain >= 1000) { | ||
| 124 | $out .= ' ' . num2vie_cardinal($remain); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | return $out; | ||
| 129 | } | ||
| 130 | |||
| 131 | # }}} | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ |