| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2008-2009 Pan Yu (xiaocong@vip.163.com). | ||||||||||||||||||||||||||||
| 3 | # All rights reserved. | ||||||||||||||||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 5 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Money::Chinese; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | $VERSION = '1.10'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | my @Chinese = qw( Áã Ò¼ ·¡ Èþ ËÁ Îé ½ Æâ °Æ ¾Á ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 8 | my $class = shift; | |||||||||||||||||||||||||||
| 22 | 8 | ▶ | my $type = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 23 | 8 | my $arg_ref = { @_ }; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | my $self = bless {}, $type; | |||||||||||||||||||||||||||
| 26 | 8 | $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 17 | ▶ | sub convert { | ||||||||||||||||||||||||||
|
Subroutine
convert: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | 17 | my $money = shift; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # replace comma and space | ||||||||||||||||||||||||||||
| 34 | 17 | $money =~ s/[,(?:\s)+]//g; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 17 | ▶ | croak "An Arabic numeral with the format of 'xxxx.xx' is expected" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless ($money =~ /^(?:\d)+(?:\.(?:\d)+)?$/); | ||||||||||||||||||||||||||||
| 38 | 15 | ▶ | croak "A non zero Arabic numeral is expected" if ($money == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 13 | $self->{integer} = undef; | |||||||||||||||||||||||||||
| 41 | 13 | $self->{decimal} = undef; | |||||||||||||||||||||||||||
| 42 | 13 | $self->{Chinese_integer} = undef; | |||||||||||||||||||||||||||
| 43 | 13 | $self->{Chinese_decimal} = undef; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 13 | ($self->{integer}, $self->{decimal}) = split /\./, $money; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 13 | ▶ | $self->_integer if ($self->{integer} != 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 13 | ▶ | $self->_decimal if (defined $self->{decimal} && $self->{decimal} != 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"decimal"} and $self->{"decimal"} != 0
MC/DC100%2/2
defined $self->{"decimal"} and $self->{"decimal"} != 0
defined $self->{"decimal"}
$self->{"decimal"} != 0
Truth table100%3/3
A and B
A= defined $self->{"decimal"}B= $self->{"decimal"} != 0
| |||||||||||||||||||||||||||||
| 49 | 13 | $self->_print; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 13 | ▶ | sub _print { | ||||||||||||||||||||||||||
|
Subroutine
_print: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 53 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 54 | 13 | my $result; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 13 | ▶ | $result = $self->{Chinese_integer} if ($self->{integer} != 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 13 | ▶ | if (defined $self->{decimal} && $self->{decimal} != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"decimal"} and $self->{"decimal"} != 0
MC/DC100%2/2
defined $self->{"decimal"} and $self->{"decimal"} != 0
defined $self->{"decimal"}
$self->{"decimal"} != 0
Truth table100%3/3
A and B
A= defined $self->{"decimal"}B= $self->{"decimal"} != 0
| |||||||||||||||||||||||||||||
| 58 | 5 | $result .= $self->{Chinese_decimal}; | |||||||||||||||||||||||||||
| 59 | }else{ | ||||||||||||||||||||||||||||
| 60 | 8 | $result .= 'Õû'; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 13 | return $result; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 5 | ▶ | sub _decimal { | ||||||||||||||||||||||||||
|
Subroutine
_decimal: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 66 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 5 | my ($cent, @cent); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | $cent[0] = substr( $self->{decimal}, 0 , 1 ); | |||||||||||||||||||||||||||
| 70 | 5 | ▶ | $cent[1] = substr( $self->{decimal}, 1 , 1 ) if (length($self->{decimal}) != 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 5 | ▶ | $cent = ($cent[0] == 0)? $Chinese[0]:$Chinese[$cent[0]] . '½Ç'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 5 | ▶ | $cent .= $Chinese[$cent[1]] . '·Ö' if ($cent[1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | $self->{Chinese_decimal} = $cent; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 12 | ▶ | sub _integer { | ||||||||||||||||||||||||||
|
Subroutine
_integer: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 78 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 79 | 12 | my (@digit, @result, $result); | |||||||||||||||||||||||||||
| 80 | 12 | my $money = $self->{integer}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 12 | ▶ | for (my $i = 0; length($money) > 0; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 23 | $digit[$i] = substr( $money, -4 , 4 ); | |||||||||||||||||||||||||||
| 84 | 23 | substr( $money, -4 , 4 ) = ''; | |||||||||||||||||||||||||||
| 85 | 23 | ▶ | $digit[$i] = '0'x(4 - length($digit[$i])) . $digit[$i] if (length($digit[$i]) != 4); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 12 | my $i = 0; | |||||||||||||||||||||||||||
| 89 | 12 | foreach (@digit) { | |||||||||||||||||||||||||||
| 90 | 23 | $i++; | |||||||||||||||||||||||||||
| 91 | 23 | ▶ | next if ($_ eq '0000'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 20 | m/(\d)(\d)(\d)(\d)/; | |||||||||||||||||||||||||||
| 93 | 20 | my $cn; | |||||||||||||||||||||||||||
| 94 | 20 | my $tail = ''; | |||||||||||||||||||||||||||
| 95 | 20 | ▶ | $cn = ($1 == 0)? $Chinese[0]:$Chinese[$1] . "Ǫ"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 20 | ▶ | $cn .= ($2 == 0)? $Chinese[0]:$Chinese[$2] . "°Û"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 20 | ▶ | $cn .= ($3 == 0)? $Chinese[0]:$Chinese[$3] . "ʰ"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 20 | ▶ | $cn .= ($4 == 0)? '':$Chinese[$4]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 20 | ▶ | if ($i%2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 7 | $tail = 'Íò'; | |||||||||||||||||||||||||||
| 101 | 7 | ▶ | $tail .= 'Áã' if ($4 == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 20 | ▶ | if($i > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 3 | $tail .= 'ÒÚ'; | |||||||||||||||||||||||||||
| 105 | 3 | ▶ | $tail .= 'Áã' if ($4 == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 3 | $tail =~ s/ÁãÒÚ/ÒÚ/; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 20 | $cn =~ s/(?:Áã)+$//; | |||||||||||||||||||||||||||
| 109 | 20 | unshift (@result, "$cn$tail"); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 12 | $result = join '',@result; | |||||||||||||||||||||||||||
| 112 | 12 | $result =~ s/(?:Áã){2,}/Áã/g; | |||||||||||||||||||||||||||
| 113 | 12 | $result =~ s/^(?:Áã)+//; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 12 | $result =~ s/(?:Áã)+$//; | |||||||||||||||||||||||||||
| 116 | 12 | $result .= 'Ԫ'; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 12 | $self->{Chinese_integer} = $result; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 6 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||