我试图使一个功能 Prettify 我的数字似乎非常小故障,例如400显示为4H,右边430显示为4.3H仍然可以接受,但403显示为4.03H,这是这里不能接受的是我的代码不知道如何解决它
编辑:随着数字越来越大,说5003,它们将显示为5.003K我希望它显示5K 编辑:随着数字越来越大,说5003,它们将显示为5.003K我希望它显示5K 基本上小数位不应该超过2 像一个列表
#include "Player.h"
Player::Player(sf::Sprite sprite, float speed) :
Entity(sprite, speed)
{}
5000 -> 5K
5500 -> 5.5k
5050 -> 5.05k
5005 -> 5K

var ranges = [
{ divider: 1000000000000000000000000000000000 , suffix: 'Dec' },
{ divider: 1000000000000000000000000000000 , suffix: 'Non' },
{ divider: 1000000000000000000000000000 , suffix: 'Oct' },
{ divider: 1000000000000000000000000 , suffix: 'Sep' },
{ divider: 1000000000000000000000 , suffix: 'Sex' },
{ divider: 1000000000000000000 , suffix: 'Quin' },
{ divider: 1000000000000000 , suffix: 'Quad' },
{ divider: 1000000000000 , suffix: 'T' },
{ divider: 1000000000 , suffix: 'B' },
{ divider: 1000000 , suffix: 'M' },
{ divider: 1000 , suffix: 'K' },
{ divider: 100 , suffix: 'H' }
];
function INP(number, round) {
for (var i = 0; i < ranges.length; i++) {
if (number >= ranges[i].divider) {
if (round == null) {
return (number / ranges[i].divider) + ranges[i].suffix;
} else {
return (number / ranges[i].divider).toFixed(round) + ranges[i].suffix;
}
}
}
return number.toString();
}
&#13;
.wrapper {
position: fixed;
top: 35%;
left: 35%;
}
&#13;
答案 0 :(得分:0)
你有两个问题。
首先,if (rounded == true)
没有做你想做的事。 rounded
是一个字符串,将字符串与布尔值进行比较会将布尔值转换为字符串,从而生成字符串"1"
,因此它等同于if (rounded == "1")
。您可以使用parseInt()
将输入转换为整数,然后使用if (rounded)
。
其次,您没有将rounded
的值传递给INP
,而是通过了true
。然后,当它在toFixed()
中使用时,它转换为整数1
,因此它总是舍入到1个小数位而不是rounded
输入中的数字。
要删除toFixed
结果上的额外尾随零,请在其上调用parseFloat()
将其转换回数字。
var ranges = [
{ divider: 1000000000000000000000000000000000 , suffix: 'Dec' },
{ divider: 1000000000000000000000000000000 , suffix: 'Non' },
{ divider: 1000000000000000000000000000 , suffix: 'Oct' },
{ divider: 1000000000000000000000000 , suffix: 'Sep' },
{ divider: 1000000000000000000000 , suffix: 'Sex' },
{ divider: 1000000000000000000 , suffix: 'Quin' },
{ divider: 1000000000000000 , suffix: 'Quad' },
{ divider: 1000000000000 , suffix: 'T' },
{ divider: 1000000000 , suffix: 'B' },
{ divider: 1000000 , suffix: 'M' },
{ divider: 1000 , suffix: 'K' },
{ divider: 100 , suffix: 'H' }
];
function INP(number, round) {
for (var i = 0; i < ranges.length; i++) {
if (number >= ranges[i].divider) {
if (round == null) {
return (number / ranges[i].divider) + ranges[i].suffix;
} else {
return parseFloat((number / ranges[i].divider).toFixed(round)) + ranges[i].suffix;
}
}
}
return number.toString();
}
var field = document.getElementById("num");
var div = document.getElementById("output");
field.onkeyup = function() {
var num = parseInt(document.getElementById("num").value, 10);
var rounded = parseInt(document.getElementById("rounded").value, 10);
if (rounded) {
var output = INP(num, rounded);
} else {
var output = INP(num);
}
div.innerHTML = "Output: " + output;
}
&#13;
.wrapper {
position: fixed;
top: 35%;
left: 35%;
}
&#13;
<div class="wrapper">
<input type="number" id="num" placeholder="number">
<br>
<input type="number" id="rounded" placeholder="round">
<div id="output"></div>
</div>
&#13;
答案 1 :(得分:0)
您的代码中存在一些错误
if (rounded == true)
数字字段的输入值永远不会变为true
,总是假的。INP(num, true)
你传递true
作为论据,与此无关。预期参数是根据您的代码编号。if (round == null)
也总是false,如果第二个输入字段为空,则值为空字符串为空,因此将其更改为if (round == '')
var ranges = [{
divider: 1000000000000000000000000000000000,
suffix: 'Dec'
}, {
divider: 1000000000000000000000000000000,
suffix: 'Non'
}, {
divider: 1000000000000000000000000000,
suffix: 'Oct'
}, {
divider: 1000000000000000000000000,
suffix: 'Sep'
}, {
divider: 1000000000000000000000,
suffix: 'Sex'
}, {
divider: 1000000000000000000,
suffix: 'Quin'
}, {
divider: 1000000000000000,
suffix: 'Quad'
}, {
divider: 1000000000000,
suffix: 'T'
}, {
divider: 1000000000,
suffix: 'B'
}, {
divider: 1000000,
suffix: 'M'
}, {
divider: 1000,
suffix: 'K'
}, {
divider: 100,
suffix: 'H'
}];
function INP(number, round) {
for (var i = 0; i < ranges.length; i++) {
if (number >= ranges[i].divider) {
if (round == '') {
return (number / ranges[i].divider) + ranges[i].suffix;
} else {
var res = (number / ranges[i].divider).toFixed(round),
fr = res - parseInt(res, 10);
return (fr > 0 ? res : parseInt(res, 10)) + ranges[i].suffix;
}
}
}
return number.toString();
}
.wrapper {
position: fixed;
top: 35%;
left: 35%;
}
<div class="wrapper">
<input type="number" id="num" placeholder="number">
<br>
<input type="number" id="rounded" placeholder="round">
<div id="output"></div>
</div>
<script>
var field = document.getElementById("num");
var div = document.getElementById("output");
field.onkeyup = function() {
var num = document.getElementById("num").value;
var rounded = document.getElementById("rounded").value;
var output = INP(num, rounded);
div.innerHTML = "Output: " + output;
}
</script>
答案 2 :(得分:0)
您可以使用10的对数计算单位前缀。应用调整并使用它。
var ranges = { 33: 'Dec', 30: 'Non', 27: 'Oct', 24: 'Sep', 21: 'Sex', 18: 'Quin', 15: 'Quad', 12: 'T', 9: 'B', 6: 'M', 3: 'K', 2: 'H' };
function INP(number, round) {
var exp = Math.floor(Math.log(number) / Math.log(10));
while (exp > 3 && !ranges[exp]) {
exp--;
}
if (!ranges[exp]) {
return number.toString();
}
return round === '' ?
(number / Math.pow(10, exp)) + ranges[exp] :
(number / Math.pow(10, exp)).toFixed(round) + ranges[exp];
}
&#13;
.wrapper {
position: fixed;
top: 35%;
left: 35%;
}
&#13;
<div class="wrapper">
<input type="number" id="num" placeholder="number">
<br>
<input type="number" id="rounded" placeholder="round">
<div id="output"></div>
</div>
<script>
var field = document.getElementById("num");
var div = document.getElementById("output");
field.onkeyup = function() {
var num = document.getElementById("num").value;
var rounded = document.getElementById("rounded").value;
var output = INP(num, rounded);
div.innerHTML = "Output: " + output;
}
</script>
&#13;