2012-04-20 81 views
3

我希望能夠在不使用數學的情況下將小數點移動到未知數量的2個位置。我知道這似乎很奇怪,但有限的精度會導致一些變化。我的JavaScript不強,但我真的很想學習如何切割一個數字,如果可能的話,這樣做。所以,我希望你真棒的人可以幫助。我可以在沒有數學的情況下在javascript中移動小數嗎?

問題:

  • 九百六十○分之五百七十五= 0.5989583333333334使用控制檯
  • 我想做出可複製和粘貼的百分比,如:59.89583333333334%
  • 如果我使用數學和繁殖由100有限,它返回59.895833333333336由於有限精度

有沒有辦法使一個字符串,只是總是m在十進制的2位右邊跳過數學?

這裏有一個小提琴也與代碼:http://jsfiddle.net/dandenney/W9fXz/

如果你想知道我爲什麼需要它,並希望精度,這對這個小工具,我爲獲得響應百分比,但不能使用計算器做:http://responsv.com/flexible-math

+0

不要忘記你正在使用的網絡瀏覽器正在與那些可能小於4K X顯示器4K像素。對於任何計算而言,可能有五六位數的精度就足夠了,對吧? – sarnold 2012-04-20 02:22:36

+0

你有沒有考慮過'toFixed'?例如'(575/960).toFixed(4)'將是0.5989(作爲一個字符串)。這可能更容易處理(更不用說漂亮了)。 – 2012-04-20 02:23:35

+0

爲什麼跳過數學的時候真的很簡單?你可以很容易地使用100除以每次向左跳2個空格var numb = 0.123; var newNumb = numb/100'。我的意思是,這真的很難嗎? – SpYk3HH 2012-04-20 02:28:19

回答

4

如果原來的數是這種類型的已知結構的,始終有至少兩位小數點右邊,你可以做這樣的:

function makePercentStr(num) { 
    var numStr = num + ""; 
    // if no decimal point, add .00 on end 
    if (numStr.indexOf(".") == -1) { 
     numStr += ".00"; 
    } else { 
     // make sure there's at least two chars after decimal point 
     while (!numStr.match(/\.../)) { 
      numStr += "0";   
     } 
    } 
    return(numStr.replace(/\.(..)/, "$1.") 
      .replace(/^0+/, "") // trim leading zeroes 
      .replace(/\.$/, "") // trim trailing decimals 
      .replace(/^$/, "0") // if empty, add back a single 0 
      + "%"); 
} 

工作演示與測試案例:http://jsfiddle.net/jfriend00/ZRNuw/

+1

但它不適用於4/2。因爲「2」變成了2而不是200%。 – 2012-04-20 02:27:50

+0

謝謝,這絕對有效!對於我這樣做的方式,我必須先將它轉換爲一個字符串,然後BOOM! – 2012-04-20 02:28:13

+0

就像我在答案的第一句話中說的那樣 - 它適用於小數點右邊有兩位數的數字,就像他們詢問的那樣。如果它需要爲所有可能的輸入數字工作,那麼它可以變得更聰明。 – jfriend00 2012-04-20 02:29:38

1

該問題要求解決問題沒有數學,但下面的解決方案涉及數學。我離開它只是一個參考

function convertToPercentage(num) { 
    //Changes the answer to string for checking 
    //the number of decimal places. 
    var numString = num + ''; 
    var length = (numString).substring(numString.indexOf(".")+1).length; 

    //if the original decimal places is less then 
    //no need to display decimals as we are multiplying by 100 
    //else remove two decimals from the result 
    var precision = (length < 2 ? 0 : length-2); 

    //if the number never contained a decimal. 
    //Don't display decimal. 
    if(numString.indexOf(".") === -1) { 
     precision = 0; 
    }   
    return (num * 100).toFixed(precision) + "%"; 
}   

Working jsFiddle這裏的測試案例爲accepted answer

0

我用,因爲浮動錯誤的風險的這種方法:

const DECIMAL_SEP = '.'; 
 

 
function toPercent(num) { 
 
    const [integer, decimal] = String(num).split(DECIMAL_SEP); 
 

 
    // no decimal, just multiply by 100 
 
    if(typeof decimal === 'undefined') { 
 
    return num * 100; 
 
    } 
 

 
    const length = decimal.length; 
 

 
    if(length === 1) { 
 
    return Number(integer + decimal + '0'); 
 
    } 
 

 
    if(length === 2) { 
 
    return Number(integer + decimal); 
 
    } 
 

 
    // more than 2 decimals, we shift the decimal separator by 2 
 
    return Number(integer + decimal.substr(0, 2) + DECIMAL_SEP + decimal.substr(2)); 
 
} 
 

 
console.log(toPercent(10)); 
 
console.log(toPercent(1)); 
 
console.log(toPercent(0)); 
 
console.log(toPercent(0.01)); 
 
console.log(toPercent(0.1)); 
 
console.log(toPercent(0.12)); 
 
console.log(toPercent(0.123)); 
 
console.log(toPercent(12.3456));

相關問題