如何在 JavaScript 中将数字格式化为百分比值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45163256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format numbers as percentage values in JavaScript?
提问by BeingSuman
Was using ExtJS for formatting numbers to percentage earlier. Now as we are not using ExtJS anymore same has to be accomplished using normal JavaScript.
之前使用 ExtJS 将数字格式化为百分比。现在因为我们不再使用 ExtJS,所以必须使用普通的 JavaScript 来完成。
Need a method that will take number and format (usually in %) and convert that number to that format.
需要一种采用数字和格式(通常以 % 表示)并将该数字转换为该格式的方法。
0 -> 0.00% = 0.00%
25 -> 0.00% = 25.00%
50 -> 0.00% = 50.00%
150 -> 0.00% = 150.00%
回答by Manish
Here is what you need.
这是你需要的。
var x=150;
console.log(parseFloat(x).toFixed(2)+"%");
x=0;
console.log(parseFloat(x).toFixed(2)+"%");
x=10
console.log(parseFloat(x).toFixed(2)+"%");
Hope this helps :)
希望这可以帮助 :)
回答by wrlee
You can use Number.toLocaleString():
您可以使用Number.toLocaleString():
var num=25;
var s = Number(num/100).toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
console.log(s);
25.00%
See documentation for toLocaleString()for more options for the format object parameter.
有关格式对象参数的更多选项,请参阅toLocaleString() 的文档。
回答by raj peer
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction(num) {
number = num.toString();;
console.log(number)
var words2 = number.split(".");
for (var i = 0; i < words2.length; i++) {
words2[i] += " ";
}
num1 = words2[0];
num2 = words2[1];
num1 = num1.trim();
if(num2==undefined){
number1 = num1+'.00%';
return number1;
}else{
num2 = num2.trim();
number1 = num1+'.'+num2+'%';
return number1;
}
}
document.getElementById("demo").innerHTML = myFunction(50.12);
</script>
</body>
</html>
回答by Calsal
I had decimal values such as 0.01235and 0.016858542that I wanted to convert to percentages 1.235% and 1.6858542% respectively. I thought it was going to be easy, just calculate (0.012858542 * 100)and I'm good to go. I was wrong, (0.012858542 * 100) = 1.2858542000000002because of decimal conversion.
我有十进制值,如0.01235和0.016858542,我想分别转换为百分比1.235%及1.6858542%。我认为这会很容易,只需计算(0.012858542 * 100)一下,我就可以开始了。我错了,(0.012858542 * 100) = 1.2858542000000002因为十进制转换。
Let's add toFixed()to the calculation and I end up with the right value.
让我们添加toFixed()到计算中,我最终会得到正确的值。
(0.012858542*100).toFixed(7) // returns "1.2858542"
(0.01235*100).toFixed(7) // returns "1.2350000"
I don't like to show four trailing zeros when they're unnecessary. One solution I thought about was to use replace()to remove all trailing zeros but I ended up using Numeral.jsinstead because it does all the work for me while it's lightweight. I can recommend it!
我不喜欢在不需要时显示四个尾随零。我想到的一个解决方案是使用replace()删除所有尾随零,但我最终使用了Numeral.js,因为它在轻量级的同时为我完成了所有工作。我可以推荐它!
import numeral from 'numeral';
numeral(0.012858542 * 100).format('0.00[0000]') // returns "1.2858542"
numeral(0.01235 * 100).format('0.00[0000]') // returns "1.235"
回答by Cristian Weiser
Transform number to percentage with X float decimal positions
使用 X 浮点小数位将数字转换为百分比
function toPercent(number, float) {
var percent = parseFloat(number * 100).toFixed(float) + "%";
return percent;
}

