| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::MON::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Mongolian | ||
| 5 | |||
| 6 | 1 | ▶ | use 5.16.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | # {{{ use block | ||
| 11 | |||
| 12 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Export::Attrs; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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.2603300'; | ||
| 21 | |||
| 22 | # }}} | ||
| 23 | |||
| 24 | # {{{ num2mon_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2mon_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 | # cardinal forms (standalone) | ||
| 36 | my @units = qw(тэг нэг хоёр гурав дөрөв тав зургаа долоо найм ес); | ||
| 37 | |||
| 38 | # combining forms (used before зуу, мянга, or as tens prefix) | ||
| 39 | my @units_combining = qw(тэг нэг хоёр гурван дөрвөн таван зургаан долоон найман есөн); | ||
| 40 | |||
| 41 | # tens standalone | ||
| 42 | my @tens = ('', '', 'хорь', 'гуч', 'дөч', 'тави', 'жар', 'дал', 'ная', 'ер'); | ||
| 43 | |||
| 44 | # tens combining (before units) | ||
| 45 | my @tens_combining = ('', 'арван', 'хорин', 'гучин', 'дөчин', 'тавин', 'жаран', 'далан', 'наян', 'ерэн'); | ||
| 46 | |||
| 47 | return $units[$positive] if ($positive >= 0 && $positive < 10); | ||
| 48 | return 'арав' if ($positive == 10); | ||
| 49 | |||
| 50 | my $out; | ||
| 51 | my $idx; | ||
| 52 | my $remain; | ||
| 53 | |||
| 54 | if ($positive > 10 && $positive < 20) { # 11 .. 19 | ||
| 55 | $remain = $positive % 10; | ||
| 56 | $out = "арван $units[$remain]"; | ||
| 57 | } | ||
| 58 | elsif ($positive > 19 && $positive < 100) { # 20 .. 99 | ||
| 59 | $idx = int ($positive / 10); | ||
| 60 | $remain = $positive % 10; | ||
| 61 | |||
| 62 | if ($remain) { | ||
| 63 | $out = "$tens_combining[$idx] $units[$remain]"; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $out = $tens[$idx]; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | elsif ($positive == 100) { # 100 | ||
| 70 | $out = 'зуу'; | ||
| 71 | } | ||
| 72 | elsif ($positive > 100 && $positive < 1000) { # 101 .. 999 | ||
| 73 | $idx = int ($positive / 100); | ||
| 74 | $remain = $positive % 100; | ||
| 75 | |||
| 76 | if ($idx == 1) { | ||
| 77 | $out = 'зуун'; | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | $out = "$units_combining[$idx] зуун"; | ||
| 81 | } | ||
| 82 | $out .= $remain ? ' ' . num2mon_cardinal($remain) : ''; | ||
| 83 | |||
| 84 | # standalone зуу if no remainder and idx == 1 already handled | ||
| 85 | } | ||
| 86 | elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999 | ||
| 87 | $idx = int ($positive / 1000); | ||
| 88 | $remain = $positive % 1000; | ||
| 89 | |||
| 90 | if ($idx == 1) { | ||
| 91 | $out = 'мянга'; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $out = num2mon_cardinal($idx) . ' мянга'; | ||
| 95 | } | ||
| 96 | $out .= $remain ? ' ' . num2mon_cardinal($remain) : ''; | ||
| 97 | } | ||
| 98 | elsif ( $positive > 999_999 | ||
| 99 | && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999 | ||
| 100 | $idx = int ($positive / 1000000); | ||
| 101 | $remain = $positive % 1000000; | ||
| 102 | |||
| 103 | if ($idx == 1) { | ||
| 104 | $out = 'нэг сая'; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $out = num2mon_cardinal($idx) . ' сая'; | ||
| 108 | } | ||
| 109 | $out .= $remain ? ' ' . num2mon_cardinal($remain) : ''; | ||
| 110 | } | ||
| 111 | |||
| 112 | return $out; | ||
| 113 | } | ||
| 114 | |||
| 115 | # }}} | ||
| 116 | |||
| 117 | # {{{ capabilities declare supported features | ||
| 118 | |||
| 119 | sub capabilities { | ||
| 120 | return { | ||
| 121 | cardinal => 1, | ||
| 122 | }; | ||
| 123 | } | ||
| 124 | |||
| 125 | # }}} | ||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ | ||