Convert RMB to C #

Source: Internet
Author: User
Tags float number

Using system;

Namespace expertlib. chinesespecial
{
# Region rmbexception
/// <Summary>
/// Incorrect RMB conversion
/// </Summary>
Public class rmbexception: system. Exception
{
Public rmbexception (string MSG)
: Base (MSG)
{
}
}
# Endregion

/// <Summary>
/// Convert the digital format of RMB to the Chinese format. The maximum size is 9 MB.
/// </Summary>
Public static class RMB
{
# Region internal Constants
Private Static string rmbuppercase = "";
Private Static string rmbunitchar = "yuanzhibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaizhao"; // indicates the corresponding number of RMB.
Private const decimal maxnumber = 9999999999999999999999999.99 m;
Private const decimal minnumber =-9999999999999999999999999.99 m;
Private Static char [] cdelim = {'.'}; // decimal separator
# Endregion

# Region convert
# Region convert <decimal>
/// <Summary>
/// Convert to the capital form of RMB
/// </Summary>
/// <Param name = "digital"> </param>
/// <Returns> </returns>
Public static string convert (decimal number)
{
Bool negativeflag = false;
Decimal rmbnumber;

Checknumberlimit (number );

Rmbnumber = math. Round (number, 2); // round the number to two decimal places.
If (rmbnumber = 0)
{
Return "Zero RMB ";
}
Else if (rmbnumber <0) // if it is a negative number
{
Negativeflag = true;
Rmbnumber = math. Abs (rmbnumber); // obtain the absolute value
}
Else
{
Negativeflag = false;
}

String Buf = ""; // store the returned results
String strdecpart = ""; // stores the processing result of the fractional part.
String strintpart = ""; // stores the processing result of the integer part.
String [] TMP = NULL;
String strdigital = rmbnumber. tostring ();

TMP = strdigital. Split (cdelim, 2); // divide the data into integers and Decimals


If (rmbnumber> = 1 m) // if it is greater than 1, the integer part must be converted.
{
Strintpart = convertint (TMP [0]);
}

If (TMP. length> 1) // returns the decimal number.
{
Strdecpart = convertdecimal (TMP [1]);
}
Else // The number without decimal digits must be an integer.
{
Strdecpart = "whole ";
}

If (negativeflag = false) // whether the value is negative
{
Buf = strintpart + strdecpart;
}
Else
{
Buf = "negative" + strintpart + strdecpart;
}
Return Buf;
}
# Endregion

# Region convert <double>
/// <Summary>
/// Convert to the capital form of RMB
/// </Summary>
/// <Param name = "Number"> </param>
/// <Returns> </returns>
Public static string convert (double number)
{
Decimal Dec;
Try
{
Dec = system. Convert. todecimal (number );
}
Catch
{
Throw new rmbexception ("cannot be converted to the standard decimal type:" + number. tostring ());
}
Return convert (DEC );
}
# Endregion

# Region convert <float>
/// <Summary>
/// Convert to the capital form of RMB
/// </Summary>
/// <Param name = "Number"> </param>
/// <Returns> </returns>
Public static string convert (float number)
{
Decimal Dec;
Try
{
Dec = system. Convert. todecimal (number );
}
Catch
{
Throw new rmbexception ("cannot be converted to the standard decimal type:" + number. tostring ());
}
Return convert (DEC );
}
# Endregion

# Region convert <int>
/// <Summary>
/// Convert to the capital form of RMB
/// </Summary>
/// <Param name = "Number"> </param>
/// <Returns> </returns>
Public static string convert (INT number)
{
Decimal Dec;
Dec = system. Convert. todecimal (number );
Return convert (DEC );
}
# Endregion

# Region convert <long>
/// <Summary>
/// Convert to the capital form of RMB
/// </Summary>
/// <Param name = "Number"> </param>
/// <Returns> </returns>
Public static string convert (long number)
{
Decimal Dec;
Dec = system. Convert. todecimal (number );
Return convert (DEC );
}
# Endregion

# Region convert <string>
/// <Summary>
/// Convert to the capital form of RMB
/// </Summary>
/// <Param name = "Number"> </param>
/// <Returns> </returns>
Public static string convert (string number)
{
Decimal Dec;
Try
{
Dec = system. Convert. todecimal (number, null );
}
Catch
{
Throw new rmbexception ("cannot be converted to the standard decimal type:" + number );
}
Return convert (DEC );
}
# Endregion
# Endregion

# Region maxsupportnumber
/// <Summary>
/// Maximum number of supported Conversions
/// </Summary>
Public static decimal maxsupportnumber
{
Get
{
Return maxnumber;
}
}
# Endregion

# Region minsupportnumber
/// <Summary>
/// Minimum supported
/// </Summary>
Public static decimal minsupportnumber
{
Get
{
Return minnumber;
}
}
# Endregion

# Region internal functions
# Region convertint
Private Static string convertint (string intpart)
{
String Buf = "";
Int length = intpart. length;
Int curunit = length;

// Process data with more than one digit
String tmpvalue = ""; // Chinese format of the current value
String tmpunit = ""; // records the Chinese unit corresponding to the current value
Int I;
For (I = 0; I <length-1; I ++, curunit --)
{
If (intpart [I]! = '0 ')
{
Tmpvalue = digtocc (intpart [I]);
Tmpunit = getunit (curunit-1 );
}
Else
{
// If the current unit is ",", You need to record it
If (curunit-1) % 4 = 0)
{
Tmpvalue = "";
Tmpunit = getunit (curunit-1 );//

}
Else
{
Tmpunit = "";

// If the current BIT is zero, you need to determine whether its next bit is zero, and then determine whether to record '0'
If (intpart [I + 1]! = '0 ')
{

Tmpvalue = "zero ";

}
Else
{
Tmpvalue = "";
}
}
}
BUF + = tmpvalue + tmpunit;
}


// Process a single bit of data
If (intpart [I]! = '0 ')
BUF + = digtocc (intpart [I]);
BUF + = "Yuan ";

Return combinunit (BUF );
}
# Endregion

# Region convertdecimal
/// <Summary>
/// Processing the fractional part
/// </Summary>
/// <Param name = "decpart"> fractional part to be processed </param>
/// <Returns> </returns>
Private Static string convertdecimal (string decpart)
{
String Buf = "";
Int I = decpart. length;

// If the decimal point is zero
If (decpart = "0") | (decpart = "00 "))
{
Return "whole ";
}

If (decpart. length> 1) // 2 digits
{
If (decpart [0] = '0') // If the corner is zero, 0.01
{
Buf = digtocc (decpart [1]) + "score"; // at this time, the score cannot be zero.
}
Else if (decpart [1] = '0 ')
{
Buf = digtocc (decpart [0]) + "Corner ";
}
Else
{
Buf = digtocc (decpart [0]) + "";
BUF + = digtocc (decpart [1]) + "points ";
}
}
Else // 1 bit
{
BUF + = digtocc (decpart [0]) + "Corner ";
}
Return Buf;
}
# Endregion

# Region getunit
/// <Summary>
/// Obtain the unit mark of the corresponding position in the Chinese format of RMB
/// </Summary>
/// <Param name = "N"> </param>
/// <Returns> </returns>
Private Static string getunit (int n)
{
Return rmbunitchar [N]. tostring ();
}
# Endregion

# Region digtocc
/// <Summary>
/// Convert a number to a corresponding Chinese character (Digital to Chinese char)
/// </Summary>
/// <Param name = "C"> numbers stored as characters </param>
/// <Returns> </returns>
Private Static string digtocc (char C)
{
Return rmbuppercase [C-'0']. tostring ();
}
# Endregion

# Region checknumberlimit
Private Static void checknumberlimit (decimal number)
{
If (number <minnumber) | (number> maxnumber ))
{
Throw new rmbexception ("beyond the convertible range ");
}
}
# Endregion

# Region combinunit
/// <Summary>
/// Merge hundreds of millions of words
/// </Summary>
/// <Param name = "RMB"> </param>
/// <Returns> </returns>
Private Static string combinunit (string RMB)
{
If (RMB. Contains ("10 million "))
{
Return RMB. Replace ("", "");
}
If (RMB. Contains (" "))
{
Return RMB. Replace ("hundreds of millions", "hundreds of millions ");
}
If (RMB. Contains (" "))
{
Return RMB. Replace ("", "MB ");
}
Return RMB;
}
# Endregion
# Endregion
}
}

 

Related Article

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.