数字转美元程序
February 15th, 2009
Categories: 杂项
本程序将数字转换为英文的美元数,如: 输入
1 | ./num2eng.pl 1,100,834.10 |
则输出:
1 | Total: Say US Dollars One Million One Hundred Thundsand Eight Hundred and Thirty-Four and Ten Cents Only. |
注意事项:
- 整数部分可以使用半角的逗号、空格、单引号、下划线、中划线分隔。
- 分隔符的位置可以任意(每3位可,每4位也可),可以任意组合(可以混合使用上述的分隔符)。
- 如果使用单引号,请注意在最外边加上双引号以免转义。
完整程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | #!/usr/bin/perl # #rex[at]zhasm[dot]com #13 Feb 2009 on perl 5.10 and Ubuntu 8.10 @single=( "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" ); @tens=( "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", ); @scale=( "", "Thundsand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion", "Undecillion", "Duodecillion", "Tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septendecillion", "Octodecillion", "Novemdecillion", "Vigintillion" ); sub dot { my ($dot)=@_; if (length($dot)==2) { #$dot=1*$dot; if ($dot<20) { return $single[$dot]; } else { $ten=int($dot/10); $extra=$dot-$ten*10; return "$tens[$ten] $single[$extra]"; } } elsif (length($dot)==1) { return "$tens[$dot]"; } } sub debug{ print @_; } sub hundred{ my ($number)=@_; $result=""; if ($number>999) { debug("too large"); return ; } my $c=int ($number/100); my $b=int(($number-$c*100)/10); my $a=$number % 10; $cc=""; $bb=""; $aa=""; if ($c) { $cc="$single[$c] Hundred"; } if ($a) { if ($b>=2) { $bb="$tens[$b]-$single[$a]"; } elsif ($b<=1) { $bb=$single[$b*10+$a]; } } if (!$a) { if ($b>=2) { $bb="$tens[$b]"; } elsif ($b<=1) #includeing the condition when $b==0; { $bb=$single[$b*10]; } } if ($c and ($a or $b)) { return "$cc and $bb"; } elsif($c and ($a==0 and $b==0)) { return $cc; } elsif(!$c) { return "and $bb"; } } sub integer{ my ($money)=@_; $index=0; $string=""; $flag=1; while ($flag) { $small=0; if ($money<1000 and $money>0) { $small=$money; $flag=0; } elsif ($money>=1000) { $small=$money % 1000; $money=int($money/1000); } if ($small) { $string=hundred($small)." $scale[$index] ".$string; } $index++; } $string =~ s/\s+$//; $string =~ s/^\s+//; $string =~ s/^and\s+//i; $string; } sub convert{ my ($digits)=@_; my $dot=0.0; my $integer=0; my $result=""; $digits =~ s/[-,_' ]//g; if ($digits =~ /(\d*)\.(\d*)/) { $integer=$1; $dot=$2; } elsif ($digits =~ /(\d+)/) { $integer=$1; } if ($integer) { $result .= integer($integer); } if ($dot and $integer) { if ($dot==1) { $result .= " and ". dot($dot)." Cent"; } elsif ($dot>1) { $result .= " and ". dot($dot)." Cents"; } } elsif ($dot and $integer==0) { if ($dot==1) { $result .= dot($dot)." Cent"; } elsif ($dot>1) { $result .= dot($dot)." Cents"; } } $result="Total: Say US Dollars ".$result." Only."; } my ($money) = @ARGV; print $line=convert($money)."\n"; |
Leave a comment
| Trackback