RMB case-insensitive Conversion

Source: Internet
Author: User

Using System;
Using System. Text;
Using System. Text. RegularExpressions;

Namespace HKH. Common
{
/// <Summary>
/// Convert the case-insensitive format of RMB
/// </Summary>
/// <Remarks> Create By Lwt on 2006/09/23
/// </Remarks>
Public class clsRMB
{
Private clsRMB ()
{
}

# Region formatting

/// <Summary>
/// Format (uppercase to lowercase)
/// </Summary>
/// <Param name = "strRMB"> </param>
/// <Returns> </returns>
Public static double Format (string strRMB)
{
Try
{
// Regular expression to verify whether the first digit is an Arabic number and determine the conversion format
// 0.15 billion ---- mixed write format
If (Regex. IsMatch (strRMB, "^ \ d "))
{
// Remove the unit of metadata
StrRMB = Regex. Replace (strRMB, "Yuan | circle ","");
Char temp = strRMB [strRMB. Length-1];
If (temp = 'wan' | temp = 'hangzhou' | temp = '661 ')
{
Return Convert. ToDouble (strRMB. Substring (0, strRMB. Length-1) * Math. Pow (10, GetExp (temp ));
}
Else
{
Return Convert. ToDouble (strRMB );
}
}
// Yiyiwu ----- capital format
Else
{
Return Eval (strRMB );
}

}
Catch
{
Return-1;
}
}

/// <Summary>
/// Format (lowercase to uppercase)
/// </Summary>
/// <Param name = "numRMB"> </param>
/// <Returns> </returns>
Public static string Format (double numRMB)
{
Try
{
If (0 = numRMB)
Return "Zero RMB ";

StringBuilder szRMB = new StringBuilder ();

// Multiply by 100 into an integer in the format for easy processing
Ulong iRMB = Convert. ToUInt64 (numRMB * 100 );

SzRMB. Insert (0, ToUpper (Convert. ToInt32 (iRMB % 100),-2 ));

// Remove the original decimal places
IRMB = iRMB/100;

Int iUnit = 0;

// Process each 4 bits as a unit segment. Therefore, divide the Unit by 10000.
While (iRMB! = 0)
{
SzRMB. Insert (0, ToUpper (Convert. ToInt32 (iRMB % 10000), iUnit ));
IRMB = iRMB/10000;
IUnit + = 4;
}

String strRMB = szRMB. ToString ();

// Format correction
StrRMB = Regex. Replace (strRMB, "zero +", "zero ");
StrRMB = strRMB. Replace ("RMB zero", "RMB whole ");
StrRMB = strRMB. Replace ("0 RMB", "RMB ");

Return strRMB. Trim ('0 ');
}
Catch
{
Return "";
}
}

# Endregion

# Region private Method

/// <Summary>
/// Calculation Expression (evaluate in uppercase)
/// </Summary>
/// <Param name = "strRMB"> </param>
/// <Returns> </returns>
Private static double Eval (string strRMB)
{
Try
{
If (null = strRMB)
Return 0;

StrRMB = Replace (strRMB, false );

If ("" = strRMB)
Return 0;

# Region uses bitwise permission for Calculation

// Basic index
Int basicExp = 0;
// Current index
Int currExp = 0;

Double numRMB = 0;

For (int I = strRMB. Length-1; I>-1; I --)
{
Char temp = strRMB [I];

If (temp = 'meta' | temp = 'wan' | temp = '6666' | temp = 'cn')
{
BasicExp = GetExp (temp );
CurrExp = 0;

Continue;
}
Else
{
If (Regex. IsMatch (temp. ToString (), "^ \ d "))
{
NumRMB = numRMB + Convert. ToInt32 (temp. ToString () * Math. Pow (10, (basicExp + currExp ));
}
Else
{
CurrExp = GetExp (temp );
}

}
}

# Endregion

Return numRMB;
}
Catch
{
Return-1;
}
}

/// <Summary>
/// Calculation Expression (returns an uppercase string in lowercase)
/// </Summary>
/// <Param name = "numRMB"> </param>
/// <Param name = "iUnit"> </param>
/// <Returns> </returns>
Private static string ToUpper (int numRMB, int iUnit)
{
Try
{
If (0 = numRMB)
{
If (iUnit =-2)
{
Return "whole ";
}

If (iUnit = 0)
{
Return "RMB ";
}

Return "zero ";
}

StringBuilder szRMB = new StringBuilder ();

String strRMB = "";

# Perform special processing on region diagonal/shard

If (iUnit =-2)
{
Int jiao = numRMB/10;
Int fen = numRMB % 10;

If (jiao> 0)
{
SzRMB. Append (jiao );
SzRMB. Append (GetUnit (-1 ));

If (fen> 0)
{
SzRMB. Append (fen );
SzRMB. Append (GetUnit (-2 ));
}
}
Else
{
SzRMB. Append (fen );
SzRMB. Append (GetUnit (-2 ));
}

Return Replace (szRMB. ToString (), true );
}

# Endregion

# The integer below region is processed properly

StrRMB = numRMB. ToString ("0000 ");

// Whether the previous digit is 0
Bool hasZero = false;

For (int I = 0; I <strRMB. Length; I ++)
{
// There are only four bits, and the highest bit is 'kil'. Therefore, the unit of 3-I below is corrected.
If (3-I)> 0)
{
If ('0 '! = StrRMB [I])
{
SzRMB. Append (strRMB [I]);
SzRMB. Append (GetUnit (3-i ));
HasZero = false;
}
Else
{
If (! HasZero)
SzRMB. Append (strRMB [I]);

HasZero = true;
}
}
// Last bit, special format processing
// If the last digit is zero, the Unit should be before zero.
Else
{
If ('0 '! = StrRMB [I])
{
SzRMB. Append (strRMB [I]);
SzRMB. Append (GetUnit (iUnit ));
HasZero = false;
}
Else
{
If (hasZero)
{
SzRMB. Insert (szRMB. Length-1, GetUnit (iUnit ));
}
Else
{
SzRMB. Append (GetUnit (iUnit ));
SzRMB. Append (strRMB [I]);
}
}
}
}

// Returns the result after uppercase is converted.
Return Replace (szRMB. ToString (), true );

# Endregion
}
Catch
{
Return "";
}
}

/// <Summary>
/// Replace uppercase letters with Arabic numerals
/// </Summary>
/// <Param name = "strRMB"> </param>
/// <Param name = "toUpper"> true -- convert to uppercase/false -- convert to lowercase </param>
/// <Returns> </returns>
Private static string Replace (string strRMB, bool toUpper)
{
If (toUpper)
{
StrRMB = strRMB. Replace ("0", "0 ");
StrRMB = strRMB. Replace ("1", "1 ");
StrRMB = strRMB. Replace ("2", "II ");
StrRMB = strRMB. Replace ("3", "");
StrRMB = strRMB. Replace ("4", "Si ");
StrRMB = strRMB. Replace ("5", "Wu ");
StrRMB = strRMB. Replace ("6", "land ");
StrRMB = strRMB. Replace ("7", "success ");
StrRMB = strRMB. Replace ("8", "success ");
StrRMB = strRMB. Replace ("9", "success ");
}
Else
{
StrRMB = strRMB. Replace ("0", "0 ");
StrRMB = strRMB. Replace ("1", "1 ");
StrRMB = strRMB. Replace ("II", "2 ");
StrRMB = strRMB. Replace ("San", "3 ");
StrRMB = strRMB. Replace ("Si", "4 ");
StrRMB = strRMB. Replace ("Wu", "5 ");
StrRMB = strRMB. Replace ("", "6 ");
StrRMB = strRMB. Replace ("comment", "7 ");
StrRMB = strRMB. Replace ("comment", "8 ");
StrRMB = strRMB. Replace ("success", "9 ");
}
Return strRMB;
}

/// <Summary>
/// Obtain the unit name
/// </Summary>
/// <Param name = "iCode"> </param>
/// <Returns> </returns>
Private static string GetUnit (int iCode)
{
Switch (iCode)
{
Case-2:
Return "points ";
Case-1:
Return "";
Case 0:
Return "RMB ";
Case 1:
Return "pick ";
Case 2:
Return "success ";
Case 3:
Return "success ";
Case 4:
Return "success ";
Case 8:
Return "";
Default:
Return "";
}
}

/// <Summary>
/// Obtain the bit weight index
/// </Summary>
/// <Param name = "cUnit"> </param>
/// <Returns> </returns>
Private static int GetExp (char cUnit)
{
Switch (cUnit)
{
Case 'shard ':
Return-2;
Case 'core ':
Return-1;
Case 'meta ':
Case 'circle ':
Return 0;
Case '10 ':
Case 'pick ':
Return 1;
Case '100 ':
Case 'handler ':
Return 2;
Case 'kilo ':
Case 'handler ':
Return 3;
Case 'wan ':
Case 'handler ':
Return 4;
Case 'yi ':
Return 8;
Default:
Return 0;
}
}

# Endregion

}
}
 

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.