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 reply to Re: english numbers to chinese numbers by kcott
in thread english numbers to chinese numbers by changlehu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.