/对数字四舍五入
//数值,精度
function round(number,precision)
{
if(isNaN(number))
number = 0;
var prec = Math.pow(10,precision);
var result = Math.round(number*prec);
result = result/prec;
return result;
}
//对数字进行格式化,保证precision位
function point(number,precision)
{
if(isNaN(number))
number = 0;
var result = number.toString();
if(result.indexOf(".")==-1)
result = result + ".";
result = result + newString("0",precision);
result = result.substring(0,precision + result.indexOf(".") + 1);
return result;
}
//java中四舍五入
public static double round(double v, int scale)
{
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, 4).doubleValue();
}
**
* @description 格式化数字:非千分位转千分位
* @param value 值
* @param count 分割位数 默认为3
* @param precision 小数点保留位数 默认为2
* @param delimiterChar 分割符 默认为','
*/
function formatFloat(value,count,precision,delimiterChar)
{
count = count==null?3:count;
precision = precision==null?2:precision;
delimiterChar = delimiterChar==null?",":delimiterChar;
//lijibin add 如果有负号则不参与格式化
var strMinus = "";
if(value<0)
{
strMinus = "-";
value = -1*value;
}
value = reverseFormatFloat(value);
var strReturn = ""; //返回值
var strValue = point(round(value,precision),precision); //格式化成指定小数位数
strReturn = strValue.substring(strValue.length-precision-1);
strValue = strValue.substring(0,strValue.length-precision-1);
while(strValue.length>count)
{
strReturn = delimiterChar + strValue.substring(strValue.length-count) + strReturn;
strValue = strValue.substring(0,strValue.length-count);
}
strReturn = strMinus + strValue + strReturn;
return strReturn;
}