Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: english numbers to chinese numbers

by kcott (Archbishop)
on Feb 09, 2017 at 08:38 UTC ( [id://1181494]=note: print w/replies, xml ) Need Help??


in reply to english numbers to chinese numbers

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1181494]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found