js中如何保留两位小数(四舍五入)
2011-08-17 15:35
555 查看
日期:2007-12-25 作者:szsoho 来源:www.szsoho.com 点击: 1326 function cheng(num,n)
{var dd=1;
var tempnum;
for(i=0;i<n;i++)
{
dd*=10;
}
tempnum=num*dd;
tempnum=Math.round(tempnum);
alert(tempnum/dd);
}
里面的两个参数:num:要转换的数据。n:转换的位数
------------------------------------------------------------------------------------------------------
/*
* ForDight(Dight,How):数值格式化函数,Dight要
* 格式化的 数字,How要保留的小数位数。
*/
function ForDight(Dight,How)
{
Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);
return Dight;
}
alert(ForDight(12345.67890,2));
</script>
-------------------------------------------------------------
function tofloat(f,dec) {
if(dec<0) return "Error:dec<0!";
result=parseInt(f)+(dec==0?"":".");
f-=parseInt(f);
if(f==0)
for(i=0;i<dec;i++) result+='0';
else {
for(i=0;i<dec;i++) f*=10;
result+=parseInt(Math.round(f));
}
return result;
}
alert(tofloat(11.30000000000000000001,2))
{var dd=1;
var tempnum;
for(i=0;i<n;i++)
{
dd*=10;
}
tempnum=num*dd;
tempnum=Math.round(tempnum);
alert(tempnum/dd);
}
里面的两个参数:num:要转换的数据。n:转换的位数
------------------------------------------------------------------------------------------------------
/*
* ForDight(Dight,How):数值格式化函数,Dight要
* 格式化的 数字,How要保留的小数位数。
*/
function ForDight(Dight,How)
{
Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);
return Dight;
}
alert(ForDight(12345.67890,2));
</script>
-------------------------------------------------------------
function tofloat(f,dec) {
if(dec<0) return "Error:dec<0!";
result=parseInt(f)+(dec==0?"":".");
f-=parseInt(f);
if(f==0)
for(i=0;i<dec;i++) result+='0';
else {
for(i=0;i<dec;i++) f*=10;
result+=parseInt(Math.round(f));
}
return result;
}
alert(tofloat(11.30000000000000000001,2))
相关文章推荐
- js中如何保留两位小数(四舍五入)
- JS保留四舍五入两小数(解决保留两位小数输入1.999变成1.100的问题)
- js-如何保留两位小数/整数的时候不出理
- 总结群里讨论的关于浮点数,如何四舍五入保留两位小数的处理方式
- JS保留两位小数(不四舍五入),向下取偶数
- 如何在js中精确除法计算并保留两位小数(不丢失精度)java针对于BigDecimal类型
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- js中如何实现保留小数几位并四舍五入
- js实现四舍五入完全保留两位小数的方法
- Office EXCEL 如何保留两位小数,四舍五入
- js 四舍五入,保留两位小数
- js 让小数四舍五入保留两位小数的函数是?
- js 保留两位小数,不要四舍五入
- Js的四舍五入和进一(保留两位小数)
- Office EXCEL 如何保留两位小数,四舍五入
- 新JS四舍五入保留两位小数
- JS保留两位小数 四舍五入函数
- C#中保留两位小数但不四舍五入的最优做法
- C#中小数点后保留两位小数,四舍五入的函数及使用方法
- js 保留两位小数,不要四舍五入