2012-04-20 37 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));

相关问题