| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::KOR::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Korean | ||
| 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 | # {{{ num2kor_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2kor_cardinal :Export { | ||
| 27 | my $positive = shift; | ||
| 28 | |||
| 29 | croak 'You should specify a number from interval [0, 999_999_999_999]' | ||
| 30 | if !defined $positive | ||
| 31 | || $positive !~ m{\A\d+\z}xms | ||
| 32 | || $positive < 0 | ||
| 33 | || $positive > 999_999_999_999; | ||
| 34 | |||
| 35 | my @digits = ('영', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구'); | ||
| 36 | |||
| 37 | return $digits[$positive] if ($positive >= 0 && $positive <= 9); # 0 .. 9 | ||
| 38 | return '십' if ($positive == 10); # 10 | ||
| 39 | |||
| 40 | my $out; # string for return value construction | ||
| 41 | my $one_idx; # index for digits array | ||
| 42 | my $remain; # remainder | ||
| 43 | |||
| 44 | if ($positive > 10 && $positive < 100) { # 11 .. 99 | ||
| 45 | $one_idx = int ($positive / 10); | ||
| 46 | $remain = $positive % 10; | ||
| 47 | |||
| 48 | $out = $one_idx > 1 ? "$digits[$one_idx]십" : '십'; | ||
| 49 | $out .= $remain ? $digits[$remain] : ''; | ||
| 50 | } | ||
| 51 | elsif ($positive > 99 && $positive < 1000) { # 100 .. 999 | ||
| 52 | $one_idx = int ($positive / 100); | ||
| 53 | $remain = $positive % 100; | ||
| 54 | |||
| 55 | $out = $one_idx > 1 ? "$digits[$one_idx]백" : '백'; | ||
| 56 | $out .= $remain ? num2kor_cardinal($remain) : ''; | ||
| 57 | } | ||
| 58 | elsif ($positive > 999 && $positive < 10_000) { # 1000 .. 9999 | ||
| 59 | $one_idx = int ($positive / 1000); | ||
| 60 | $remain = $positive % 1000; | ||
| 61 | |||
| 62 | $out = $one_idx > 1 ? "$digits[$one_idx]천" : '천'; | ||
| 63 | $out .= $remain ? num2kor_cardinal($remain) : ''; | ||
| 64 | } | ||
| 65 | elsif ($positive > 9_999 && $positive < 100_000_000) { # 10000 .. 99_999_999 | ||
| 66 | $one_idx = int ($positive / 10000); | ||
| 67 | $remain = $positive % 10000; | ||
| 68 | |||
| 69 | $out = num2kor_cardinal($one_idx) . '만'; | ||
| 70 | $out .= $remain ? num2kor_cardinal($remain) : ''; | ||
| 71 | } | ||
| 72 | elsif ($positive > 99_999_999 | ||
| 73 | && $positive < 1_000_000_000_000) { # 100_000_000 .. 999_999_999_999 | ||
| 74 | $one_idx = int ($positive / 100_000_000); | ||
| 75 | $remain = $positive % 100_000_000; | ||
| 76 | |||
| 77 | $out = num2kor_cardinal($one_idx) . '억'; | ||
| 78 | $out .= $remain ? num2kor_cardinal($remain) : ''; | ||
| 79 | } | ||
| 80 | |||
| 81 | return $out; | ||
| 82 | } | ||
| 83 | |||
| 84 | # }}} | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ |