2016-01-11 62 views
0

由於標題狀態我試圖將英寸轉換爲英尺,而不是像5.02顯示小數,我希望它顯示5'2「例如。我想創建一個從英寸到英尺的轉換器,但只顯示小數點

這裏是GUI的樣子enter image description here

我想要的「轉換腳」顯示,他們的標準ft'inches」格式。

我的代碼HTML

<form class="form-inline"> 
             <div class="form-group"> 
              <label for="width">Width:</label> 
              <input type="text" class="form-control" id="width" placeholder=""> 
             </div> 
             <div class="form-group"> 
              <label for="height">Height:</label> 
              <input type="text" class="form-control" id="height" placeholder=""> 
             </div> 
             <div class="form-group"> 
              <label for="measurement">Measurement:</label> 
              <select class="form-control" id="measurement" placeholder=""> 
               <option value="1">Inches</option> 
               <option value="2">Feet</option> 
              </select> 
             </div> 
             <div class="form-group"> 
              <input type="button" class="btn btn-info" value="Submit" onclick="myFunction()"> 
             </div> 
             <div class="form-group"> 
              <input type="button" class="btn btn-danger" value="Clear" onclick="clearFunction()"> 
             </div> 
            </form> 

            <table class="table table-bordered table-background" id="contentTable"> 
             <thead> 
              <tr> 
               <th>Type</th> 
               <th>Surface Area</th> 
               <th>Conversion to Inches</th> 
               <th>Conversion to Feet</th> 
              </tr> 
             </thead> 
             <tbody> 
              <tr> 
               <td id="type"></td> 
               <td id="area"></td> 
               <td id="conInch"></td> 
               <td id="conFeet"></td> 
              </tr> 
             </tbody> 
            </table> 

這裏是我的JavaScript

function myFunction() 
      { 
       var width = document.getElementById('width').value; 
       var height = document.getElementById('height').value; 
       var measurment; 
       var area; 
       var widthFeet; 
       var heightFeet; 
       var widthInches; 
       var heightFeet; 
       var type; 

       if(document.getElementById('measurement').value == 1){ 
        measurment = 1; 
        type = "Inches" 
        area = width * height; 
        widthFeet = width/12; 
        heightFeet = height/12; 
        widthInches = width; 
        heightInches = height; 
       } else if(document.getElementById('measurement').value == 2){ 
        measurment = 2; 
        type = "Feet" 
        area = width * height; 
        widthInches = width * 12; 
        heightInches = height * 12; 
        widthFeet = width; 
        heightFeet = height; 
       } 

       document.getElementById('type').innerHTML = type; 
       document.getElementById('area').innerHTML = area + "(" + width + "x" + height + ")"; 
       document.getElementById('conInch').innerHTML = widthInches + " X " + heightInches; 
       document.getElementById('conFeet').innerHTML = widthFeet.toFixed(2) + " X " + heightFeet.toFixed(2); 
      } 

      function clearFunction(){ 
       document.getElementById('type').innerHTML = ""; 
       document.getElementById('area').innerHTML = ""; 
       document.getElementById('conInch').innerHTML = ""; 
       document.getElementById('conFeet').innerHTML = ""; 
      } 
+0

在編程中查找模數...它是javascript中的%運算符。 –

+0

是的,我知道%是什麼以及如何使用它,但我不認爲這是一個解決方案,因爲其餘的不是英寸。所以12 x 13英寸顯示1 x 1.0833333,其餘的這裏是不正確的。 – Charles

+0

13 X 14應該是1'1「X 1'2」... 2「來自14%12 –

回答

1

假設輸入變量寬度高度都是英寸,我覺得你可以得到您要求的號碼如下:

widthFeet = Math.floor(width/12); 
widthInches = width%12; 
heightFeet = Math.floor(height/12); 
heightInches = height%12; 
相關問題