javascript - 如何将小数四舍五入到两位?

标签 javascript jquery

所以我所有的小数都四舍五入到两位。但是,当我单击“键盘或鼠标”图像时,小数位大约为十位数(例如:36.900000000000006)。我该如何阻止这个。我尝试过使用“.toFixed()和.toPrecision()。

这里是该页面的链接...比创建 fiddle 容易得多。

http://www.ootpik.info/lauren/ootpik5/gethardware.html

我添加了一个 fiddle :http://jsfiddle.net/lolsen7/8uwGH/3/

脚本

$(document).ready(function() {

    $(".part,.extra").mouseover(function() {
        if(this.className !== 'part selected') {
        $(this).attr('src', 'images/placeholder/get_hardware/' + this.id + '.png');
      }
        $(this).mouseout(function(){
                if(this.className !== 'part selected') {
                    $(this).attr('src', 'images/placeholder/get_hardware/' + this.id + '_grey.png');
                }
            });
    });

    var list = document.getElementById("list");
    var summaryTotal = document.getElementById("summaryTotal");

    var touchtotal = 13.89;
    var minitotal = 19.47;
    var constant = touchtotal + minitotal;
    $('#finaltotal').text(constant);

    //station one
    var keyboard2 = 59;
    var mouse2 = 59;
    var printer = 345;
    var scale2 = 530;
    var display = 175;

    var single = 132;
    var multi = 260;
    var scannerTotal = 0;

    var cash = 100;
    var storage = 125;
    var registerTotal = 0;

    //extras
    var ipad = 349;
    var ipadTotal = 0;

    var mobilePrinter = 570;
    var mobileTotal = 0;

    var printer2 = 345;
    var printer2Total = 0;


    $(".part").click(function(){

        var name = this.id;
        var liname = this.alt;
        var total = parseFloat((eval(name) * 1.2) /40);


        if(this.className != 'part selected')
        {
            $(this).attr('src','images/placeholder/get_hardware/' + name + '.png');

            if(name !== 'multiBar' && name !== 'singleBar' && name !== 'storageDrawer' && name !== 'cashDrawer' )
            {
                //generate list items
                var li = document.createElement("li");
                li.setAttribute("id",name + "_li");
                li.appendChild(document.createTextNode(liname));
                list.appendChild(li);

                //touchscreen and mac mini pricing
                constant = constant + total;
                $('#finaltotal').text(constant);
            }
            //Scanners if clicked on
            else if(name == "multiBar" || name == "singleBar"){
                $(".tooltip").show();
                $("input").change(function(e){
                    if($(this).attr("checked", "true")) {
                        if(this.value == "one") {
                            // hide and show images based on radio selection
                            $('#singleBar').attr('src','images/placeholder/get_hardware/singleBar.png');
                            $('#singleBar').show();
                            $('#singleBar').toggleClass('selected');
                            $('#multiBar').hide();

                            //list item
                            if($('#single_li').length > 0)
                            {
                                $('#single_li').remove();
                            }
                            if($('#multi_li').length > 0)
                            {
                                $('#multi_li').remove();
                            }

                            li = document.createElement("li");
                            li.setAttribute("id","single_li");
                            li.appendChild(document.createTextNode("Single-line Barcode Scanner"));
                            list.appendChild(li);

                            //pricing
                            if(scannerTotal !== 0)
                            {
                                constant = constant - scannerTotal;
                            }

                            scannerTotal = (single * 1.2) /40;
                            constant = constant + scannerTotal;
                            $('#finaltotal').text(constant);



                        }else if(this.value == "two") {
                            //hide and show images based on radio selection
                            $('#multiBar').attr('src','images/placeholder/get_hardware/multiBar.png');
                            $('#multiBar').show();
                            $('#singleBar').hide();

                             //list item
                            if($('#multi_li').length > 0){
                                $('#multi_li').remove();
                            }
                            if($('#single_li').length > 0){
                                $('#single_li').remove();
                            }

                            li = document.createElement("li");
                            li.setAttribute("id","multi_li");
                            li.appendChild(document.createTextNode("Multi-line Barcode Scanner"));
                            list.appendChild(li);

                            if(scannerTotal !== 0){
                                constant = constant - scannerTotal;
                            }
                            scannerTotal = (multi * 1.2) /40;
                            constant = constant + scannerTotal;
                            $('#finaltotal').text(constant);

                        }
                    }

                });
            }
            //if register is clicke on
            else if(name =="storageDrawer" || name == "cashDrawer"){
                $('.tooltip2').show();
                $('input').change(function(){
                   if($(this).attr("checked","true")) {
                       if(this.value == "three") {
                           //hide and show images based on radio button selection
                           $('#cashDrawer').attr('src','images/placeholder/get_hardware/cashDrawer.png');
                           $('#cashDrawer').show();
                           $("#storageDrawer").hide();

                           //list items
                           if($('#cash_li').length > 0){
                               $('#cash_li').remove();
                           }
                           if($('#storage_li').length > 0){
                               $('#storage_li').remove();
                           }

                           li = document.createElement("li");
                           li.setAttribute('id','cash_li');
                           li.appendChild(document.createTextNode("Cash Drawer"));
                           list.appendChild(li);

                           //pricing
                           if(registerTotal !== 0){
                               constant = constant - registerTotal;
                           }
                           registerTotal = (cash * 1.2)/40;
                           constant = constant + registerTotal;
                           $('#finaltotal').text(constant);



                       }else if(this.value == "four") {
                           $('#storageDrawer').attr('src','images/placeholder/get_hardware/storageDrawer.png');
                           $('#storageDrawer').show();
                           $('#cashDrawer').hide();

                           //list items
                           if($('#storage_li').length > 0){
                               $('#storage_li').remove();
                           }
                           if($('#cash_li').length > 0){
                               $('#cash_li').remove();
                           }

                           li = document.createElement('li');
                           li.setAttribute('id','storage_li');
                           li.appendChild(document.createTextNode("Premium Cash Drawer"));
                           list.appendChild(li);

                           //pricing
                           if(registerTotal !== 0){
                               constant = constant - registerTotal;
                           }
                           registerTotal = (storage * 1.2)/40;
                           constant = constant + registerTotal;
                           $('#finaltotal').text(constant);
                       }
                   } 
                });
            }

            $(this).toggleClass('selected');

        } 
        else if (this.className == 'part selected')
        {
            $(this).attr('src','images/placeholder/get_hardware/' + name + '_grey.png');

            if(name !== 'multiBar' && name !== 'singleBar' && name !== 'storageDrawer' && name !== 'cashDrawer')
            {
                constant = constant - total;
                $('#finaltotal').text(constant);
                $("#" + name + "_li").remove();
            }
            //Scanners
            else if(name == 'multiBar' || name == 'singleBar')
            {
                //Hides Tooltip when de-selecting item
                $(".tooltip").hide(); 
               // removes pricing
               console.log(scannerTotal);
                constant = constant - scannerTotal;
                scannerTotal = 0;
                $('#finaltotal').text(constant);

                //Removes any List items 
                if($('#multi_li').length > 0)
                {
                    $('#multi_li').remove();
                }
                if($('#single_li').length > 0)
                {
                    $('#single_li').remove();
                }
                //Sets Inputs to deselected

                $("input[name='group1']").attr("checked",false);
            }
            //cash drawers || registers
            else if(name == 'storageDrawer' || name == 'cashDrawer')
            {
                $('.tooltip2').hide();
                //removes pricing
                console.log(registerTotal);
                constant = constant - registerTotal;
                registerTotal = 0;
                $('#finaltotal').text(constant);

                //remove any list items
                if($('#storage_li').length > 0)
                {
                    $('#storage_li').remove();
                }
                if($('#cash_li').length > 0)
                {
                    $('#cash_li').remove();
                }
                //Sets Inputs to deselected

                $("input[name='group2']").attr("checked",false);
            }
            $(this).toggleClass('selected');
        }
  });

    $(".numbers-row").append('<div class="inc buttons">+</div><div class="dec buttons">-</div>');
    $(".buttons").on("click", function() {
    var $buttons = $(this);
    var oldValue = $buttons.parent().find("input").val();
    if ($buttons.text() == "+") {
        var newVal = parseFloat(oldValue) + 1;
        if(newVal > 2 ){
            return;
        }
    } else {
       // Don't allow decrementing below zero
      if (oldValue > 0) {
        var newVal = parseFloat(oldValue) - 1;
        } else {
        newVal = 0;
      }
      }
    $buttons.parent().find("input").val(newVal);

    if($buttons.parent().attr('class') == 'numbers-row ipad'){
      if($('#ipad_mini_extra').length > 0){
        $('#list').children('#ipad_mini_extra').remove();
        //remove pricing
        if(newVal == 0){
            constant = constant - ipadTotal;
            console.log(ipadTotal);
            $('#finaltotal').text(constant);
        }
      }
      if(newVal !== 0){
        $('#ipad').attr('src',"images/placeholder/get_hardware/ipad.png");
        $('#list').append('<li id="ipad_mini_extra">' + newVal + ' iPad mini(s) </li>');

        //add pricing
        if(ipadTotal !== 0){
            constant = constant - ipadTotal;
        }
        if(newVal == 1){
           ipadTotal = (ipad * 1.2) /40;
           console.log(ipadTotal);
           constant = constant + ipadTotal;
         $('#finaltotal').text(constant);
        }

         if(newVal == 2){
             ipadTotal = ((ipad * 2) * 1.2)/40;
             console.log(ipadTotal)
             constant =constant + ipadTotal;
             $('#finaltotal').text(constant);
         }

      }
      else{
        $('#ipad').attr('src',"images/placeholder/get_hardware/ipad_grey.png");
      }
    }
     if($buttons.parent().attr('class') == 'numbers-row mobile')
    {
      if($('#mobile_extra').length > 0)
      {
        $('#list').children('#mobile_extra').remove();
        //remove pricing
        if(newVal == 0){
            constant = constant - mobileTotal;
            console.log(mobileTotal);
            $('#finaltotal').text(constant);
        }
      }
      if(newVal !== 0)
      {
        $('#mobilePrinter').attr('src',"images/placeholder/get_hardware/mobilePrinter.png");
        $('#list').append('<li id="mobile_extra">' + newVal + ' Mobile Printer(s) </li>');

        //add pricing
        if(mobileTotal !== 0){
            constant = constant - mobileTotal;
        }
        if(newVal == 1){
           mobileTotal = (mobilePrinter * 1.2) /40;
           console.log(mobileTotal);
           constant = constant + mobileTotal;
         $('#finaltotal').text(constant);
        }

         if(newVal == 2){
             mobileTotal = ((mobilePrinter * 2) * 1.2)/40;
             console.log(mobileTotal)
             constant =constant + mobileTotal;
             $('#finaltotal').text(constant);
         }
      }
      else
      {
        $('#mobilePrinter').attr('src',"images/placeholder/get_hardware/mobilePrinter_grey.png");
      }
    }
    if($buttons.parent().attr('class') == 'numbers-row printer2')
    {
      if($('#printer2_extra').length > 0)
      {
        $('#list').children('#printer2_extra').remove();
        //remove pricing
        if(newVal == 0){
            constant = constant - printer2Total;
            console.log(printer2Total);
            $('#finaltotal').text(constant);
        }
      }
      if(newVal !== 0)
      {
        $('#printer2').attr('src',"images/placeholder/get_hardware/printer2.png");
        $('#list').append('<li id="printer2_extra"> ' + newVal + ' Kitchen/Bar/Expo Printer(s) </li>');

        //add pricing
        if(printer2Total !== 0){
           constant = constant - printer2Total;
        }
        if(newVal == 1){
           printer2Total = (printer2 * 1.2) /40;
           console.log(printer2Total);
           constant = constant + printer2Total;
         $('#finaltotal').text(constant);
        }

         if(newVal == 2){
             printer2Total = ((printer2 * 2) * 1.2)/40;
             console.log(printer2Total)
             constant =constant + printer2Total;
             $('#finaltotal').text(constant);
         }
      }
      else
      {
        $('#printer2').attr('src',"images/placeholder/get_hardware/printer2_grey.png");
      }
    }
  });
});

最佳答案

我无法阅读所有内容并判断您是否这样做,但您需要确保要显示的数字实际上是数字而不是字符串:

var myNumber = parseFloat(yourNumber); //36.222211112221;

然后你可以使用 toFixed(2);

console.log(myNumber.toFixed(2));  // 36.22

关于javascript - 如何将小数四舍五入到两位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17866798/

相关文章:

javascript - 从 for 循环创建数组对象

javascript - 按下按钮,文本框中的值增加

javascript - React redux 搜索过滤器不显示过滤内容?

javascript - 如何知道事件发生时触发了什么函数(或代码片段)?

javascript - 使用 setInterval 更改背景颜色

javascript - 按钮点击不会在第二次点击时触发

javascript - 是否可以像字符串一样操作/操作函数?

javascript - 修改页面 DOM 的 Firefox 插件

javascript - 在javascript或jquery中单独调用特定函数

javascript - 模态背景覆盖的 Bootstrap 模型