千分号格式化
Code
//格式化千分号
function UCFormatCurrency(obj)
{
if(obj == null)
{
return;
}
var dataValue = obj.value
var strArray = null;//数组
var nowValue = "";
var retValue = "";
var len = 0;
var comma = 0;
var divided = 0;
strArray = dataValue.split('.');
nowValue = strArray[0];
nowValue = nowValue.replace("-", "");
len = nowValue.length;
comma = len % 3;
divided = parseInt(len / 3, 10);
if (comma == 0)
{
retValue = nowValue.substr(0, 3);
for (var i = 1; i < divided; i++)
{
retValue += ",";
retValue += nowValue.substr(i * 3, 3);
}
}
else
{
retValue = nowValue.substr(0, comma);
for (var i = 0; i < divided; i++)
{
retValue += ",";
retValue += nowValue.substr(comma + i * 3, 3);
}
}
if (dataValue.substr(0,1) == "-")
{
retValue = "-" + retValue;
}
if (strArray.length > 1)
{
retValue = retValue + "." + strArray[1];
}
obj.value = retValue;
}
//格式化千分号
function UCFormatCurrency(obj)
{
if(obj == null)
{
return;
}
var dataValue = obj.value
var strArray = null;//数组
var nowValue = "";
var retValue = "";
var len = 0;
var comma = 0;
var divided = 0;
strArray = dataValue.split('.');
nowValue = strArray[0];
nowValue = nowValue.replace("-", "");
len = nowValue.length;
comma = len % 3;
divided = parseInt(len / 3, 10);
if (comma == 0)
{
retValue = nowValue.substr(0, 3);
for (var i = 1; i < divided; i++)
{
retValue += ",";
retValue += nowValue.substr(i * 3, 3);
}
}
else
{
retValue = nowValue.substr(0, comma);
for (var i = 0; i < divided; i++)
{
retValue += ",";
retValue += nowValue.substr(comma + i * 3, 3);
}
}
if (dataValue.substr(0,1) == "-")
{
retValue = "-" + retValue;
}
if (strArray.length > 1)
{
retValue = retValue + "." + strArray[1];
}
obj.value = retValue;
}
c#版:
Code
#region 千位符格式化
/// <summary>
/// 千位符格式化
/// </summary>
/// <param name="dataValue"></param>
/// <returns></returns>
private string FormatCurrency(string dataValue)
{
if (string.IsNullOrEmpty(dataValue))
{
return string.Empty;
}
dataValue = dataValue.Replace(",", "");
string[] strArray = null;
string nowValue = string.Empty;
string retValue = string.Empty;
int len = 0;
int comma = 0;
int divided = 0;
try
{
strArray = dataValue.Split('.');
nowValue = strArray[0];
nowValue = nowValue.Replace("-", "");
len = nowValue.Length;
comma = len % 3;
divided = len / 3;
if (comma == 0)
{
retValue = nowValue.Substring(0, 3);
for (int i = 1; i < divided; i++)
{
retValue += ",";
retValue += nowValue.Substring(i * 3, 3);
}
}
else
{
retValue = nowValue.Substring(0, comma);
for (int i = 0; i < divided; i++)
{
retValue += ",";
retValue += nowValue.Substring(comma + i * 3, 3);
}
}
if (dataValue.StartsWith("-") == true)
{
retValue = "-" + retValue;
}
if (strArray.Length > 1)
{
retValue = retValue + "." + strArray[1];
}
return retValue;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 千位符格式化
/// <summary>
/// 千位符格式化
/// </summary>
/// <param name="dataValue"></param>
/// <returns></returns>
private string FormatCurrency(string dataValue)
{
if (string.IsNullOrEmpty(dataValue))
{
return string.Empty;
}
dataValue = dataValue.Replace(",", "");
string[] strArray = null;
string nowValue = string.Empty;
string retValue = string.Empty;
int len = 0;
int comma = 0;
int divided = 0;
try
{
strArray = dataValue.Split('.');
nowValue = strArray[0];
nowValue = nowValue.Replace("-", "");
len = nowValue.Length;
comma = len % 3;
divided = len / 3;
if (comma == 0)
{
retValue = nowValue.Substring(0, 3);
for (int i = 1; i < divided; i++)
{
retValue += ",";
retValue += nowValue.Substring(i * 3, 3);
}
}
else
{
retValue = nowValue.Substring(0, comma);
for (int i = 0; i < divided; i++)
{
retValue += ",";
retValue += nowValue.Substring(comma + i * 3, 3);
}
}
if (dataValue.StartsWith("-") == true)
{
retValue = "-" + retValue;
}
if (strArray.Length > 1)
{
retValue = retValue + "." + strArray[1];
}
return retValue;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion