Practical numeric conversion (C #) Conversion

Source: Internet
Author: User

From http://www.cnitblog.com/hj627/

"

 Using  System;
Using System. Collections. Generic;
Using System. text;

Namespace CW
{

Public Class Cash
{
Private Static String Moneynum = " " ;
Private Static String Moneyunit = " Million, billion " ;
Private Static String Cashunit = " Yuan Jiao Ming " ;
Private Static String [] Beforereplace = New String [] {Moneynum [ 0 ]. Tostring () + Moneynum [ 0 ]. Tostring (),
Moneyunit [
7 ]. Tostring () + Moneynum [ 0 ]. Tostring () + Moneyunit [ 3 ]. Tostring (),
Moneynum [
0 ]. Tostring () + Moneyunit [ 3 ]. Tostring (),
Moneynum [
0 ]. Tostring () + Moneyunit [ 7 ]. Tostring (),
Moneyunit [
7 ]. Tostring () + Moneyunit [ 3 ]. Tostring (),
Moneynum [
0 ]. Tostring () + Moneynum [ 0 ]. Tostring (),
Moneynum [
0 ]. Tostring () + Cashunit [ 1 ]. Tostring () + Moneynum [ 0 ]. Tostring () + Cashunit [ 2 ]. Tostring (),
Moneynum [
0 ]. Tostring () + Cashunit [ 2 ]. Tostring ()
};
Private Static String [] Afterreplace = New String [] {Moneynum [ 0 ]. Tostring (),
Moneyunit [
7 ]. Tostring () + Moneynum [ 0 ]. Tostring (),
Moneyunit [
3 ]. Tostring () + Moneynum [ 0 ]. Tostring (),
Moneyunit [
7 ]. Tostring () + Moneynum [ 0 ]. Tostring (),
Moneyunit [
7 ]. Tostring () + Moneynum [ 0 ]. Tostring (),
Moneynum [
0 ]. Tostring (),
String. empty,
String. Empty
};

/**/ /// <Summary>
/// String replacement method. The other is a recursive method based on four numbers.
/// Create by HJ 2007-10-21
/// </Summary>
/// <Param name = "moneynum"> </param>
/// <Returns> </returns>
Public Static String Getcash ( Decimal Moneynum)
{
String Intnum, point;
/**/ /// Integer
Intnum = Convert. tostring (math. Floor (moneynum ));
/**/ /// Decimal part
Point = Convert. tostring (math. Floor (moneynum * 100 ));
Point
= Point. substring (point. Length - 2 );

/**/ /// Calculate integer
For ( Int I = 0 ; I < Moneynum. length; I ++ )
{
Intnum
= Intnum. Replace (I. tostring (), moneynum [I]. tostring ());
}
Int Intnumlength = Intnum. length;
For ( Int I = Intnumlength - 1 ; I > 0 ; I -- )
{
/**/ /// Add Unit by number of digits
/// If the value is zero, no unit is added, but and need to be added.
If (Intnum [I - 1 ] = Moneynum [ 0 ] && (Intnumlength - I + 7 ) % 8 ! = 3 && (Intnumlength - I + 7 ) % 8 ! = 7 ) Continue ;
Intnum
= Intnum. insert (I, moneyunit [(intnumlength - I + 7 ) % 8 ]. Tostring ());
}

/**/ /// Unit of plus angle and score
Intnum + = Cashunit [ 0 ]. Tostring () + Moneynum. substring (convert. toint16 (point [ 0 ]. Tostring ()), 1 ) + Cashunit [ 1 ]. Tostring () + Moneynum. substring (convert. toint16 (point [ 1 ]. Tostring ()), 1 ) + Cashunit [ 2 ]. Tostring ();

/**/ /// Replace zero-> 0 billion 0 thousand-> 0 thousand zero, 0 billion-> zero,-> zero,-> "", zero point-> "", zero point-> zero point, and then call it again to ensure the situation after replacement of 0 thousand million
For ( Int I = 0 ; I < Beforereplace. length; I ++ )
{
While (Intnum. indexof (beforereplace [I]) >- 1 )
{
Intnum
= Intnum. Replace (beforereplace [I], afterreplace [I]);
}
}

/**/ /// Remove the last Zero
If (Intnum. endswith (moneynum [ 0 ]. Tostring () intnum = Intnum. substring ( 0 , Intnum. Length - 1 );

Return Intnum + Cashunit [ 3 ]. Tostring ();

}

}
}

"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.