The stupid question is the question not asked | |
PerlMonks |
Re: english numbers to chinese numbersby kcott (Archbishop) |
on Feb 09, 2017 at 08:38 UTC ( [id://1181494]=note: print w/replies, xml ) | Need Help?? |
G'day changlehu, Welcome to the Monastery. I saw the comments[1, 2] regarding the formatting of your post. I took at look at the source HTML: you'd clearly made some effort to improve the formatting. Your code had reasonable indentation (which we can't see when it's posted as paragraph text); you'd also added <br /> tags to the end of every line. So, when posting ASCII code or data, do use <code> tags; otherwise, use <pre> tags. Do follow the links provided: they provide more details on this. Anyway, as this was your first post, and you'd obviously made some effort to get it right, here's how your posted code would have looked with <pre> tags (and without any <br /> tags): my %cn_numbers=( 1=>"一", 2=>"二", 3=>"三", 4=>"四", 5=>"五", 6=>"六", 7=>"七", 8=>"八", 9=>"九", ); my $number=shift; my $cn_number; if ($number>=10000){ my $n=$number/10000; $n=~s/\..*//; $cn_number=$cn_numbers{$n}."万"; $number=$number % 10000; } if ($number>=1000){ my $n=$number/1000; $n=~s/\..*//; $cn_number.=$cn_numbers{$n}."千"; $number=$number % 1000; } if ($number>=100){ my $n=$number/100; $n=~s/\..*//; $cn_number.=$cn_numbers{$n}."百"; $number=$number % 100; } if ($number>=10){ my $n=$number/10; $n=~s/\..*//; $cn_number.=$cn_numbers{$n}."十"; $number=$number % 10; } if ($number>0){ $cn_number.=$cn_numbers{$number}; } #补零 if ($cn_number=~/万/ && $cn_number!~/千/){ $cn_number=~s/万/万零/; } if ($cn_number=~/千/ && $cn_number!~/百/){ $cn_number=~s/千/千零/; } if ($cn_number=~/百/ && $cn_number!~/十/){ $cn_number=~s/百/百零/; } $cn_number=~s/一十/十/g; print "$cn_number\n"; When I first read your post yesterday, I really had no idea what the code was doing because I have zero knowledge of the language. However, when researching something entirely different earlier today (CSS Counter Styles Level 3), I came across this section, which explained all the characters and how the numbers were built up. That may be of interest to others who wish to understand your code more fully. — Ken
In Section
Cool Uses for Perl
|
|