博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS保留两位小数

Posted on 2011-12-19 16:03  itcfj  阅读(1224)  评论(0编辑  收藏  举报

JS保留两位小数

JS保留两位小数

 

对于一些小数点后有多位的浮点数,我们可能只需要保留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 *100)/100;

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*100)/100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0)
{
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2)
{
s_x += '0';
}
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")    // 返回 NaNparseFloat("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*1000)/1000;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0)
    {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 3)
    {
        s_x += '0';
    }
    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>