为了账号安全,请及时绑定邮箱和手机立即绑定

关于javascript中四舍五入的

关于javascript中四舍五入的

qq_Dreamy_旧城_0 2017-06-27 12:11:01
//html代码<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>访问当当购物车页面节点</title>    <link type="text/css" rel="stylesheet" href="css/cartStyle.css" /></head><body><div>    <div>        <img src="images/dd_logo.jpg"><span onclick="close_plan();">关闭</span>    </div>    <div id="cartList">        <ul>            <li>¥<input type="text" name="price" value="21.90"></li>            <li><input type="button" name="minus" value="-" onclick="minus(0);"><input type="text" name="amount" value="1"><input type="button" name="plus" value="+" onclick="plus(0);"></li>            <li id="price0">¥21.90</li>            <li><p  onclick="collection();">移入收藏</p><p onclick="del();">删除</p></li>        </ul>        <ul>            <li>¥<input type="text" name="price" value="24.00"></li>            <li><input type="button" name="minus" value="-" onclick="minus(1);"><input type="text" name="amount" value="1"><input type="button" name="plus" value="+" onclick="plus(1);"></li>            <li id="price1">¥24.00</li>            <li><p  onclick="collection();">移入收藏</p><p onclick="del();">删除</p></li>        </ul>        <ol>            <li id="totalPrice">&nbsp;</li>            <li><span onclick="accounts();">结 算</span></li>        </ol>    </div>    <div id="dismy">           </div></div><script type="text/javascript" src="js/shopping.js"></script><script type="text/javascript">function accounts(){var cartList=document.getElementById("cartList");var price1=cartList.firstElementChild.lastElementChild.previousElementSibling.innerHTML;//var pr1=price1.toFixed(2);//(这里想要保留小数点后两位,但因为有¥字符串,没办法截取?)//alert(pr1);var price2=cartList.lastElementChild.previousElementSibling.lastElementChild.previousElementSibling.innerHTML;var jie=cartList.lastElementChild.firstElementChild.innerHTML;var money = jie.substring(1);var xin="您本次购买的商品信息如下:<br/>白岩松:白说:"+price1+"<br/>岛上书店:"+price2+"<br/>商品共计:"+money;document.getElementById("dismy").innerHTML= xin;}</script></body></html>//js代码/** * Created by zongjuan.wang on 2016/6/1. *//*关闭窗口*/function close_plan(){    window.close();}function collection(){    var flag=confirm("移入收藏后,将不再购物车显示,是否继续操作?");    if(flag==true){        alert("移入收藏成功!");    }}function del(){    var flag=confirm("您确定要删除商品吗?");    if(flag==true){        alert("删除成功!");    }}function minus(num){    var prices=document.getElementsByName("price")[num].value;    var count=parseInt(document.getElementsByName("amount")[num].value)-1;    if(count<1){        alert("不能再减了,再减就没有啦!");    }    else{        document.getElementsByName("amount")[num].value=count;        var totals=parseFloat(prices*count);        document.getElementById("price"+num).innerHTML="¥" +totals;        total();    }}function plus(num){    var prices=document.getElementsByName("price")[num].value;    var count=parseInt(document.getElementsByName("amount")[num].value)+1;    document.getElementsByName("amount")[num].value=count;    var totals=parseFloat(prices*count);    document.getElementById("price"+num).innerHTML="¥" +totals;    total();}function total(){    var prices=document.getElementsByName("price");    var count=document.getElementsByName("amount");    var sum=0;    for(var i=0; i<prices.length;i++){       sum+=prices[i].value*count[i].value;    }    document.getElementById("totalPrice").innerHTML="¥" +sum;}total();被加粗的代码哪里,麻烦会的同学,帮忙解答
查看完整描述

1 回答

已采纳
?
堇延未七

TA贡献36条经验 获得超19个赞

获取到的price1应该是一个字符串,用substr截出子串(24.00),然后用它去调用tofixed()方法

查看完整回答
反对 回复 2017-06-27
  • 1 回答
  • 0 关注
  • 1556 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信