Regex a code that converts the amount into RMB capital

  • 2020-05-16 06:47:59
  • OfStack

 
public static string ConvertToChineseMoney(double money) 
{ 
if (money < 0) 
throw new ArgumentOutOfRangeException(" parameter money It can't be negative! "); 
string s = money.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"); 
s = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}"); 
return Regex.Replace(s, ".", delegate(Match m) { return " Negative yuan empty zero remark 3 boss wu lu and pure nine empty empty empty air separation Angle up company � � million jing note Zi exa "[m.Value[0] - '-'].ToString(); }); 
} 

Call the method:
 
Random r = new Random(); 
for (int i = 0; i < 10; i++) 
{ 
double m = r.Next() / 100.0; 
Response.Write(m + "---- " + ConvertToChineseMoney(m) + "<br/>"); 
} 

Operation results:

5970370.36-- five hundred and ninety seven, three hundred and seventy three cents
6521548.6-- six hundred and fifty-two, one thousand five hundred and forty-eight yuan
12179380.35-- one thousand two hundred and seven hundred and nine thousand three hundred and eight hundred and thirty-five
15742735.47-- one thousand five hundred and seven hundred and four -- two thousand seven hundred and three hundred and fifty four cents
9236554.09-- nine million two hundred and three, six thousand five hundred and forty four and nine cents
9706980.49-- nine hundred and seventy, six thousand nine hundred and eight hundred and forty nine cents
11873034.16-- one thousand one hundred and eighty seven, three thousand three hundred and four one point six cents
21141596.22-- two thousand one hundred and one hundred and four. One thousand five hundred and nine hundred and six yuan and two point two
17040243.44-- one thousand seven hundred and four, two hundred forty three and forty four cents
5358585.54-- five hundred and thirty-five, eight thousand five hundred and eight hundred and five hundred and five cents

Related articles: