金额格式转换(千分位、小数点等)

/**
* @param strValue: wstring going to be unified, return unified wstring
* @param amountType: amount type for unify 0-,. 1-. 2-NA 3-,
* @return true-normal false-error
*/
int CExSamples::unify_money(wstring& strValue, int amountType)
{
	if(strValue.size() < 1)
		return false;
	wstring strOriValue = strValue;
	wstring strPunct = L"";
	wstring strNum = L"";
	bool negative = false;
	if(strValue[0] == L'-' || strValue[0] == L'_' || strValue[0] == L'一')
		negative = true;
	for(int i = (int)strValue.length() - 1; i >= 0; i--)
	{
		if(L'0' <= strValue[i] && strValue[i] <= L'9')
		{
			strNum = strValue[i] + strNum;
			if(i - 1 >= 0 && (strValue[i-1] == L',' || strValue[i-1] == L'.'))
			{
				strPunct = strValue[i-1] + strPunct;
			}
			else
			{
				strPunct = L'n' + strPunct;//占位符
			}
		}
	}
	if(strPunct.length() > 0)
		strPunct[0] = L'n';//删除在数字之前的符号
	if(strPunct.length() != strNum.length())
		return false;
	if(strNum.length() < 1)
		return false;
	if(amountType == 0)
	{//,.
		int nDotPos = -1;
		for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 3; i--)
		{//在后三位中找点
			if(strPunct[i] == L'.')
				nDotPos = i;
		}
		if(nDotPos < 0)
		{
			for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 2; i--)
			{//在后两位处找逗
				if(strPunct[i] == L',')
				{
					strPunct[i] = L'.';//改为点
					nDotPos = i;
				}
			}
		}
		int prePunct = (int)strNum.length();
		int index = (int)strNum.length() - 1;
		if(nDotPos > 0)
		{
			index = nDotPos - 1;
			prePunct = nDotPos;
		}

		for(int i = index; i >= 0; i --)
		{//处理逗号
			if(strPunct[i] == L'.' || strPunct[i] == L',')
			{
				strPunct[i] = L',';//把自己改成逗号
				prePunct = i;
			}
			if(prePunct - i >= 2)
			{//补充逗号
				if(i - 2 >= 0 && strPunct[i-1] != L',' && strPunct[i-1] != L'.' && strPunct[i-2] != L',' && strPunct[i-2] != L'.')
						strPunct[i-1] = L',';
			}
		}
	}
	else if(amountType == 1)
	{// .
		int nDotPos = -1;
		for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 3; i--)
		{
			if(strPunct[i] == L'.')
				nDotPos = i;
		}
		if(nDotPos < 0)
		{
			for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 2; i--)
			{
				if(strPunct[i] == L',')
				{
					strPunct[i] = L'.';//改为点
					nDotPos = i;
				}
			}
		}
		int index = (int)strNum.length() - 1;
		if(nDotPos > 0)
		{
			index = nDotPos - 1;
		}

		for(int i = index; i >= 0; i --)
		{//删除点号前的任何符号
			strPunct[i] = L'n';
		}
	}
	else if(amountType == 2)
	{// NA
		int nDotPos = -1;
		for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 3; i--)
		{
			if(strPunct[i] == L'.')
				nDotPos = i;
		}
		if(nDotPos < 0)
		{
			for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 2; i--)
			{
				if(strPunct[i] == L',')
				{
					strPunct[i] = L'.';//改为点
					nDotPos = i;
				}
			}
		}
		int index = (int)strNum.length() - 1;
		if(nDotPos > 0)
		{
			index = nDotPos - 1;
		}
		for(int i = index; i >= 0; i --)
		{//删除点号前的任何符号
			strPunct[i] = L'n';
		}
		strNum = strNum.substr(0, nDotPos);//删除.后的数字
		strPunct = strPunct.substr(0, nDotPos);
		if(strNum.length() != strPunct.length())
			return false;
	}
	else if(amountType == 3)
	{// ,
		int nDotPos = -1;
		for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 3; i--)
		{
			if(strPunct[i] == L'.')
				nDotPos = i;
		}
		if(nDotPos < 0)
		{
			for(int i = (int)strNum.length() - 1; i >= 0 && i >= (int)strNum.length() - 2; i--)
			{
				if(strPunct[i] == L',')
				{
					strPunct[i] = L'.';//改为点
					nDotPos = i;
				}
			}
		}
		int prePunct = (int)strNum.length();
		int index = (int)strNum.length() - 1;
		if(nDotPos > 0)
		{
			index = nDotPos - 1;
			prePunct = nDotPos;
		}

		for(int i = index; i >= 0; i --)
		{//处理逗号
			if(strPunct[i] == L'.' || strPunct[i] == L',')
			{
				strPunct[i] = L',';//把自己改成逗号
				prePunct = i;
			}
			if(prePunct - i >= 2)
			{//补充逗号
				if(i - 2 >= 0 && strPunct[i-1] != L',' && strPunct[i-1] != L'.' && strPunct[i-2] != L',' && strPunct[i-2] != L'.')
						strPunct[i-1] = L',';
			}
		}
		strNum = strNum.substr(0, nDotPos);//删除.后的数字
		strPunct = strPunct.substr(0, nDotPos);
		if(strNum.length() != strPunct.length())
			return false;
	}
	wstring strUniValue = L"";
	if(strNum.length() != strPunct.length())
		return false;
	for(int i = (int)strNum.length() - 1; i >= 0; i--)
	{
		strUniValue = strNum[i] + strUniValue;
		if(strPunct[i] == L',' || strPunct[i] == L'.')
			strUniValue = strPunct[i] + strUniValue;
	}
	if(negative)
		strUniValue = L'-' + strUniValue;
	strValue = strUniValue;
	return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值