Mature and stable numeric conversion to Chinese Character amount capital character Function

Source: Internet
Author: User

During the development of the grid ++ report tool, a function is required to convert numbers into uppercase Chinese characters. Some works contributed by friends are also found on the internet, but there are some shortcomings after careful reading. There was no way at last, so we had to combine the ideas of all parties and re-invent the wheel. The entire function is still relatively complex, but there is a huge user base test of grid ++ report, which can ensure that the function for converting the capital amount of Chinese characters is mature, stable, and efficient. Here, I hope to help my friends who need it and avoid the trouble of re-inventing the wheel.

CodeIt is written in VC ++, as follows:

# Include "stdafx. H"
# Include <string>
# Include <atlstr. h>

Using namespace STD;
Using namespace ATL;

Const char const_str_zeroyuan [] = "Zero yuan ";
Const char const_str_bigamtnumber [] = "";
Const char const_str_hznegative [] = "negative ";
Const cstring strunit = "yuanqibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaihui ";
Const cstring strnumber = const_str_bigamtnumber;
Const cstring strotherunit = "integral corner ";

String tobigmoney (double dmoney)
{
Const double zerolowrange =-0.005; //-0.004449;
Const double zerohighrange = 0.005; // 0.004449;
If (zerolowrange <dmoney & dmoney <zerohighrange)
Return const_str_zeroyuan; // "Zero RMB ";

Bool negative = false;
If (dmoney <= zerolowrange)
{
Negative = true;
Dmoney =-dmoney;
}

// There is no exception to the excess part here. Users should note that (in reality there will not be such a huge amount of money)
Cstring strmoney;
Strmoney. Format ("%. 2f", dmoney );

// Divide the number into integer and decimal parts
Int NPOs = strmoney. Find (".");
Int nlength = strmoney. getlength ();
If (NPOs <0)
NPOs = nlength;

Cstring strreturnvalue;
Int ncount = 0;
Bool bzero = false;
Bool bneedlevel = false; // identifies a segment to determine whether a segment name is required, such as million or million

// Reverse identification of the integer part
For (INT I = NPOs-1; I> = 0; I --)
{
Char CH = strmoney. getat (I );

If (ncount % 4 = 0 & ncount> 0)
{
// If the number to be processed is fourth (), or eighth (), the segment is required.
Bneedlevel = true;
}

If (CH = '0 ')
{
// Only identify the zero position of the hacker, mainly taking into account the particularity of the pick-up, that is, 10 read, will not read the zero
// If (ncount % 4! = 0)
// Bzero = true;
Bzero = true; // get rid of % 4 to make the 102000 read Statement: million RMB per second. 9,050,155,001-> million yuan
}
Else
{
Cstring strtemp (strreturnvalue );
Strreturnvalue = strnumber. mid (CH-0x30) * 2, 2 );

If (ncount> 0)
{
Strreturnvalue + = strunit. mid (ncount * 2, 2 );
If (ncount % 4! = 0 & bneedlevel)
{
// Determine whether the segment name needs to be read, such as or million
Strreturnvalue + = strunit. mid (INT (ncount/4) * 8, 2 );
}
Bneedlevel = false;
}

If (bzero)
{< br> // zero is set only when there are digits lower than the bit currently processed.
If (! Strtemp. isempty ()
strreturnvalue + = strnumber. Left (2);
bzero = false;
}

Strreturnvalue + = strtemp;
}

Ncount ++;
}

// If there is no integer, the Canadian dollar is not required.
If (! Strreturnvalue. isempty ())
Strreturnvalue + = strunit. Left (2 );

// The following process is implemented after the decimal point. You do not need to continue reading if it is set to zero.
Bool ballzero = true;
If (NPOs <nlength)
{
If (nlength> 2)
Nlength = 2;
For (INT I = 0; I <nlength; I ++)
If (strmoney. getat (NPOs + I + 1 )! = '0 ')
Ballzero = false;
}

If (ballzero)
{
Strreturnvalue + = strotherunit. Left (2 );
}
Else
{
// Corner-based processing. If there is no integer, no zero angle is required.
Char CH = strmoney. getat (NPOs + 1 );
If (Ch! = '0' |! Strreturnvalue. isempty ())
Strreturnvalue + = strnumber. mid (CH-0x30) * 2, 2 );
If (Ch! = '0 ')
Strreturnvalue + = strotherunit. mid (1*2, 2 );

// Process the score. If there is no score, add an integer to the corner'
Ch = strmoney. getat (NPOs + 1 + 1 );
If (Ch! = '0 ')
{
Strreturnvalue + = strnumber. mid (CH-0x30) * 2, 2 );
Strreturnvalue + = strotherunit. mid (2*2, 2 );
}
Else
{
Strreturnvalue + = strotherunit. Left (2 );
}
}

If (negative)
Strreturnvalue = const_str_hznegative + strreturnvalue;

Return string (strreturnvalue );
}


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.