js保留2位小数toFixed(xxxx)

 var  a   =   9.39393;
alert(a.toFixed()); alert(Number.toFixed(9.39393));
返回的是9.

对于一些小数点后有多位的浮点数,我们可能只需要保留2位,但js没有提供这样直接的函数,所以我们得自己写函数实现这个功能,代码如下:

function changeTwoDecimal(x)
{
var f_x = parseFloat(x);
if (isNaN(f_x))
{
alert('function:changeTwoDecimal->parameter error');
return false;
}
f_x = Math.round(f_x *)/; return f_x;
}

功能:将浮点数四舍五入,取小数点后2位

用法:changeTwoDecimal(3.1415926) 返回 3.14

changeTwoDecimal(3.1475926) 返回 3.15

js保留2位小数(强制)

对于小数点位数大于2位的,用上面的函数没问题,但是如果小于2位的,比如:

changeTwoDecimal(3.1),将返回 3.1,如果你一定需要3.10这样的格式,那么需要下面的这个函数:

function changeTwoDecimal_f(x)
{
var f_x = parseFloat(x);
if (isNaN(f_x))
{
alert('function:changeTwoDecimal->parameter error');
return false;
}
f_x = Math.round(f_x*)/;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < )
{
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + )
{
s_x += '';
}
return s_x;
}

功能:将浮点数四舍五入,取小数点后2位,如果不足2位则补0,这个函数返回的是字符串的格式

用法:changeTwoDecimal(3.1415926) 返回 3.14

changeTwoDecimal(3.1) 返回 3.10

另:

parseFloat 方法

返回由字符串转换得到的浮点数。

parseFloat(numString)

必选项 numString 参数是包含浮点数的字符串。

说明

parseFloat 方法返回与 numString 中保存的数相等的数字表示。如果 numString 的前缀不能解释为浮点数,则返回 NaN (而不是数字)。

parseFloat("abc")    // 返回 NaN
parseFloat("1.2abc")   // 返回 1.2

可以用 isNaN 方法检测 NaN

示例1:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script> function changetText() {
document.getElementById("txtB").value = document.getElementById("txtA").value; } </script> </head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtA" runat="server" onblur="changetText()"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server"></asp:TextBox> </div>
</form>
</body>
</html>

示例2:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script>     //把数值保存3位有效值
function changeThreeDecimal_f(varObj)
{
var x = varObj.value;
var f_x = parseFloat(x);
if (isNaN(f_x))
{
return;
}
f_x = Math.round(f_x*)/;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < )
{
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + )
{
s_x += '';
}
varObj.value = s_x;
} </script> </head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtA" runat="server" onblur="changeThreeDecimal_f(this)"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server"></asp:TextBox> </div>
</form>
</body>
</html>

最新文章

  1. 百度编辑器 ueditor 内容编辑自动套P标签,及p标签 替换
  2. HTML5实战——svg学习
  3. Sql Server 高频,高并发访问中的键查找死锁解析
  4. python爬取并计算成绩
  5. 【技术贴】删除360快捷搜索 ctrl+ctrl
  6. RHCS集群
  7. textChanged(*)重点
  8. Delphi回调函数及其使用
  9. Ajax实践之用户是否存在
  10. Fragment和Activity(转)
  11. Android 中基于 Binder的进程间通信
  12. VMware Workstation 12 Pro 之安装XP系统
  13. 003-005:Java平台相关的面试题
  14. 我的工具:Ping工具
  15. Vs 开发时无法断点问题
  16. hive join on 条件 与 where 条件区别
  17. python删除文件和文件夹
  18. Mongodb 集群实战
  19. crm作业知识点集合[一]
  20. 【代码笔记】iOS-产生随机数

热门文章

  1. CodeForces 18C
  2. HTML 背景图片自适应
  3. 洛谷 P1858 多人背包
  4. linux常用命令及安装软件命令
  5. 我的PHP之旅--XML初步
  6. 自定义Angular指令与jQuery实现的Bootstrap风格数据双向绑定的单选&amp;多选下拉框
  7. ASP.NET 4.5.256 has not been registered on the Web server
  8. hdu 4794 FIb求循环节
  9. QT的Paint 系统
  10. objective C中数据持久化方式1--对象归档