博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS保留两位小数
阅读量:4598 次
发布时间:2019-06-09

本文共 2680 字,大约阅读时间需要 8 分钟。

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")    // 返回 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" "">

<html xmlns="" >

<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" οnblur="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" "">

<html xmlns="" >

<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" οnblur="changeThreeDecimal_f(this)"></asp:TextBox>
    <asp:TextBox ID="txtB" runat="server"></asp:TextBox>

    </div>

    </form>
</body>
</html>

 

转载于:https://www.cnblogs.com/yuhanzhong/archive/2012/04/25/2469754.html

你可能感兴趣的文章
zabbix监控tcp/nginx/memcache连接数自定义监控shell
查看>>
Django 中间件
查看>>
Appium + python - 监控appium server start
查看>>
一段采访——团队作业 #2
查看>>
AtCoder Grand Contest 004 : Colorful Slimes (DP)
查看>>
js中的apply和call API
查看>>
Linux常用网站
查看>>
软件开发人员必须具备的20款免费的windows下的工具(转载)
查看>>
MyBatis源码探索
查看>>
python 迭代
查看>>
File查看目录
查看>>
去除ActionBar的方法
查看>>
STM8S——Universal asynchronous receiver transmitter (UART)
查看>>
Flink - state管理
查看>>
Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
查看>>
ArcGIS JS Demo
查看>>
webservice发布问题,部署iis后调用不成功
查看>>
Koch 分形,海岸线,雪花
查看>>
ubuntu系统下Python虚拟环境的安装和使用
查看>>
IOS7开发~新UI学起(二)
查看>>