2 qq 15290009 qq_15290009 于 2016.03.17 15:20 提问

在哪里添加toFixed精确!!!!!!!!!!!!!!
js

图片说明

出现了这种情况 按理说 不能的啊 但是不知道原因 所以想给他精切到俩位

//更新合计和差距

tdAges.each(function (index) {

tdSums.eq(index).html((parseFloat(this.innerHTML) || 0) + (sum[index] || 0));

tdDiffs.eq(index).html((parseFloat(this.innerHTML) || 0) - (sum[index] || 0));

});

我是在这里加toFixed 但是他会出现 033.00 这样的结果 所以问问你们在那加

js贴在下面吧

3个回答

fubo1990
fubo1990   2016.03.17 16:31
已采纳

像钱这样的数据。一般都保留两位小数,所以一般所有用的钱的数字都要通过这样进行控制小数位。parseFloat(原生数字).toFixed(2)

fubo1990
fubo1990 嗯。解决了就好。
一年多之前 回复
qq_15290009
qq_15290009 好了 把 parseFloat 换成int 就好了
一年多之前 回复
qq_15290009
qq_15290009 我试了 不对 都加上了 是这么个结果0.00.000
一年多之前 回复
fubo1990
fubo1990 对的。都得加上。你上面我看到好几个地方在用,所有都加上再试一试。就好了
一年多之前 回复
qq_15290009
qq_15290009 我就加了一个
一年多之前 回复
qq_15290009
qq_15290009 不是 一个大神给写的 都加上呢?
一年多之前 回复
fubo1990
fubo1990 parseFloat(this.innerHTML).toFixed(2) 这类代码都加上
一年多之前 回复
fubo1990
fubo1990 代码不是你写的吗?
一年多之前 回复
qq_15290009
qq_15290009 我把能加的地方都加了一遍 不对的
一年多之前 回复
qq_15290009
qq_15290009 额 我也知道 但是我找不到在哪加了
一年多之前 回复
qq_15290009
qq_15290009   2016.03.17 15:21


<body>
  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script>
    var food = {
  请选择:{
   请选择:[]
  },
        谷物: {

           大米: [343,7.7,0.6,76.8,0,0.6,11,1.1,121,97,2.4,0.19,34,1.45,2.5,0,0.33,0.08,0.2,20,8,0,1.01,220,0,0,0,3.8,0.6,1.5], 
     小麦: [350,9.4,1.4,75,0,2.8,25,0.6,162,127,0.2,0.26,32,0.2,0.2,11,0.24,0.07,0.05,17.3,0,0,0.3,185,0,0,0,8,0.7,0.47],
     玉米: [196,4,2.3,4.2,0,10.5,1,0.5,187,238,1.1,0.25,96,0.9,1.63,63,0.21,0.06,0.11,15,10,0,1.7,216,1,0,0.34,12,1.9,1.6],
     小米: [359,9.2,3.2,73.3,0,1.6,9,5.6,240,239,9,0.54,107,2.08,4.74,17,0.67,0.12,0.18,73,0,0,3.63,143,0,0,0.19,29,1.7,1.6],
     糯米: [345,7.3,1.4,77.5,0,0.8,26,6.7,155,231,1.5,0.25,49,1.54,2.71,0,0.19,0.03,0.04,23,0,0,1.29,120,0,0,0,7,0.5,2],
     黑米: [339,8.9,2.2,70.8,0,2.8,12,1.6,179,256,7.1,0.15,147,3.8,3.2,19,0.41,0.33,0.54,104,32,0,0.6,270,0,0,3.87,15,0.2,2.3],
     燕麦: [367,15,6.7,61.6,0,5.3,186,7,291,214,3.7,0.45,177,2.59,4.31,420,0.3,0.13,0.16,54.4,0,0,307,73,0,0,0,25,1.1,1.2],
     薏米: [357,12.8,3.3,69.1,0,2,42,3.6,217,238,3.6,0.29,88,1.68,3.07,416,0.33,0.5,0.07,150,0,0,2.08,0,0,0,0,16,0.16,2],
     芡实: [144,9.8,0.2,75,0,0.4,9,0.4,110,134,2.3,0.12,8,1.72,2.28,10,0.4,0.08,0.02,110,6,0,0,0,0,0,0.02,18,0.52,2.5],
     荞麦: [324,9.3,2.3,66.5,0,6.5,47,6.2,297,401,4.7,0.56,258,0.56,3.62,13,0.28,0.16,0.35,0.02,0,0,4.4,0.2,0,0.33,2.4,44,1.54,2.2]

        },
        //....更加多食物种类
    };
    //岁数数组中的营养和上面列出的一致
     var age = {
        请选择:  [],
        '1-7岁': {
            "女": [1600,55,30,0,300,0,800,10,700,1500,900,8.66,25,90,1,0.8,30,20,600,0.7,0.7,0.6,1.2,70,10,28.5,16,200,3,9],
            "男": [1700,55,30,0,300,0,800,10,700,1500,900,8.66,25,90,1,0.8,30,20,600,0.7,0.7,0.6,1.2,70,10,25.5,16,200,3,9]
        },
    };
</script>
食品:<select id="food1"></select><select id="food2"></select><input type="button" value="添加" onclick="addFood()" /><br />
年龄段:<select id="age"></select>  <select id="sex">
                                                                                                      <option >请选择</option>
                                                                                                      <option value="男">男</option>
                                                                                                      <option value="女">女</option>
                                                                                                      </select><br />
<table id="tab_Test3" border="1" width="1000px">
                                                                                                        <tr class='tabTh'>
                                                                                                            <td width="100px">食品名称</td>
                                                                                                            <td width="100px">热量</td>
                                                                                                            <td width="100px">蛋白质</td>
                                                                                                            <td width="100px">脂肪</td>
                                                                                                            <td width="100px">碳水化合物</td>
                                                                                                            <td width="100px">胆固醇</td>
                                                                                                            <td width="200px">膳食纤维</td>
                                                                                                            <td width="100px">钙</td>
                                                                                                            <td width="100px">铁</td>
                                                                                                            <td width="100px">磷</td>
                                                                                                            <td width="100px">钾</td>
                                                                                                            <td width="100px">钠</td>
                                                                                                            <td width="100px">铜</td>
                                                                                                            <td width="100px">镁</td>
                                                                                                            <td width="100px">锌</td>
                                                                                                            <td width="100px">硒</td>
                                                                                                            <td width="100px">维生素A</td>
                                                                                                            <td width="100px">维生素B1</td>
                                                                                                            <td width="100px">维生素B2</td>
                                                                                                            <td width="100px">维生素B6</td>
                                                                                                            <td width="100px">维生素B12</td>
                                                                                                            <td width="100px">维生素C</td>
                                                                                                            <td width="100px">维生素D</td>
                                                                                                            <td width="100px">维生素E</td>
                                                                                                            <td width="100px">生物素</td>
                                                                                                            <td width="100px">维生素K</td>
                                                                                                            <td width="100px">维生素P</td>
                                                                                                            <td width="100px">胡萝卜素</td>
                                                                                                            <td width="100px">叶酸</td>
                                                                                                            <td width="100px">泛酸</td>
                                                                                                            <td width="100px">烟酸</td>
                                                                                                            <td width="100px">操作</td>
                                                                                                        </tr>
                                                                                                        <tr align='center' id="trSum">
                                                                                                            <td>合计</td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                        </tr>
                                                                                                        <tr align='center' id="trAge">
                                                                                                            <td>人体正常值</td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                             <td></td>
                                                                                                        </tr>
                                                                                                        <tr align='center' id="trDiff">
                                                                                                            <td>差距值</td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                            <td></td>
                                                                                                             <td></td>
                                                                                                        </tr>
                                                                                                        <tbody id="tbFood"></tbody>
                                                                                                    </table>
<script>
    var sFood1 = $('#food1'), sFood2 = $('#food2'), sAge = $('#age'), sSex = $('#sex')
         //去掉第一个名列和最后一个操作列
        , tdSums = $('#trSum td:gt(0)').not(':last')
        , tdAges = $('#trAge td:gt(0)').not(':last')
        , tdDiffs = $('#trDiff td:gt(0)').not(':last')
        //////
        , tbFood = $('#tbFood')
    //初始化食品一级分类及事件
    for (attr in food) sFood1.append('<option value="' + attr + '">' + attr + '</option>');
    sFood1.change(function () {
        var arr = food[this.value];
        sFood2.html('');
        for (attr in arr) sFood2.append('<option value="' + attr + '">' + attr + '</option>');
    }).trigger('change');//触发一级分类加载2级分类
    //初始化年龄段
    for (attr in age) sAge.append('<option value="' + attr + '">' + attr + '</option>');
    sAge.add('#sex').change(function () {///////
        var arr = sAge.val() == '请选择' ? age['请选择'] : age[sAge.val()][sSex.val()];
        //sAge1.html('');
        //for (attr in arr) sAge1.append('<option value="' + attr + '">' + attr + '</option>');
        tdAges.each(function (index) { this.innerHTML = arr[index]; });

        updateSumDiff();
    });//触发onchange更新trAge中的td数值

    function updateSumDiff() {//更新合计和差距
        var sum = [];//每营养小计
        tbFood.find('tr').each(function () {
            $('td:gt(0)', this).not(':last').each(function (index) {
                sum[index] = (parseFloat(this.innerHTML) || 0) + (sum[index] || 0);
            });
        });
        //更新合计和差距
        tdAges.each(function (index) {
            tdSums.eq(index).html((parseFloat(this.innerHTML) || 0) + (sum[index] || 0));
            tdDiffs.eq(index).html((parseFloat(this.innerHTML) || 0) - (sum[index] || 0));
        });
    }
    function addFood() {
        var arr = food[sFood1.val()][sFood2.val()];
        var s = '<tr><td>' + sFood2.val() + '</td>';
        for (var i = 0; i < arr.length; i++) s += '<td>' + arr[i] + '</td>';
        s += '<td><input type="button" value="删除" onclick="removeFood(this)"/></td></tr>';
        tbFood.append(s);
        updateSumDiff();
    }
    function removeFood(o) { $(o).closest('tr').remove(); updateSumDiff(); }
</script>



</body>





qq_15290009
qq_15290009   2016.03.17 16:20

怎么 没有人回答啊 js 是个完整的例子

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!