2013-07-25 33 views
0

所以我所有的小數都被四捨五入爲兩個地方。但是,當我點擊「鍵盤或鼠標」圖像時,小數位數大約爲十位數字(例如:36.900000000000006)。我該如何阻止這一點。我一直在使用」 .toFixed(),和.toPrecision試過()。如何保持小數四捨五入到兩個地方?

這裏是該頁面的鏈接...更容易做到這一點比創建一個小提琴。

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

我添加了一個小提琴: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"); 
     } 
    } 
    }); 
}); 
+0

的可能重複[JavaScript的:正好用兩個小數數字格式(http://stackoverflow.com/questions/1726630/javascript-格式化數字,精確到小數點) – cfs

+1

我不認爲我們需要代碼的整個內容,只是相關的部分。 – tymeJV

+1

「*這裏是鏈接到頁面......比創建小提琴更容易做到這一點*」 - 它可能更容易,但它對我們來說沒有什麼用處,因爲我們不能那麼容易地看到或編輯,您網站上的JavaScript。 –

回答

3

我不能看了都覺得,如果你這樣做或不說,但你必須確保電話號碼要顯示實際上是一個數字,而不是一個字符串:

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

那麼你可以使用toFixed(2);

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

這比我的更乾淨 – Plato

0

編輯 - 與number.toFixed() @ericjbasti的解決方案是比這更好的。無論如何都留給子孫後代:)


你只是向用戶提供數字?

嘗試這種(感覺這麼髒,但是這是JS :)

var num = 36.900000000000006; 
var string = String(num); 
var left = string.split('.')[0] || ""; 
var right = string.split('.')[1] || ""; 
right = right.slice(0,2); // trim any chars after and including index 2 
if(right !== ""){ 
    var short = left + '.'+ right; 
} else { 
    var short = left; 
}; 
// short = +short; // cast to Number if necessary 
+0

我已經添加了一個小提琴,希望這會有所幫助。 [jsfiddle](http://jsfiddle.net/lolsen7/8uwGH/3/) – Lauren

+0

我建議你定義一個函數'formatFinalTotal(content)',它應用這裏描述的方法之一,然後設置'#finaltotal' div到格式化的值,而不是直接設置'$('#finaltotal').text(content)' – Plato