将功能Clickrate更改为十进制形式时会出现问题
我正在尝试更改用户获得硬币"的比率.在我正在制作的增量点击游戏上,我遇到了一个问题,我想在哪里制作游戏,因此用户单击时只能获得零钱,而获得的却是完整的钱.例如,我想将其更改为0.01而不是1.因此,这是Java语言,我不知道该怎么做,所以对您有所帮助.
I am trying to change the rate at which users receive "coins" on a incremental clicking game I am making, and I am having a problem where I want to make it so users only get fractions of coins when they click, but instead they are getting full coins. For example, I want to change it to 0.01 Coins Per Click, instead of 1. This is in Javascript and I cant quite figure out how to do this, so anything helps.
这是我的Javascript代码:
Here is my Javascript code:
<script>
//variables
var clicks = 0; //increment this by one every click
var auto_clicks = 0; //automatically click once per second
var cost = 1; //the cost of this should increase exponentially
var upgrade_speed = 0; //the level of the speed up upgrade
var click_rate = 1000; //ms between each autoclick
var interval_auto; //storing our interval here so we can update it
var click_increment = 1; //how many clicks per click
//functions
function update_total_clicks() { //updates the number of clicks
var e = document.getElementById("total_clicks");
e.innerHTML = clicks;
}
function buy_something(c, button) {
if (clicks < c) {
button.className = 'btn btn-danger';
setTimeout(function() {
var e = document.getElementsByClassName("btn-danger")[0];
e.className = 'btn btn-success';
}, 1000);
return false;
}
clicks -= c;
return true;
}
function update_workers() {
var e2 = document.getElementById("time_period");
e2.innerHTML = parseFloat(click_rate / 1000.0).toFixed(2);
clearInterval(interval_auto);
interval_auto = setInterval(function() {
clicks += auto_clicks;
update_total_clicks();
}, click_rate);
}
//click events
document.getElementById("click").onclick = function() {
clicks = parseFloat(clicks) + parseFloat(click_increment);
update_total_clicks(); //updates the text
};
document.getElementById("buy_click").onclick = function() {
if (!buy_something(cost, this)) return;
auto_clicks++;
cost = Math.pow(2, auto_clicks); //new cost
update_total_clicks();
var e = document.getElementById("clicks_per_second");
e.innerHTML = auto_clicks;
var e2 = document.getElementById("buy_click");
e2.innerHTML = 'Buy for ' + cost;
var e2 = document.getElementById("autoclicker_level");
e2.innerHTML = 'lvl ' + auto_clicks;
};
document.getElementById("upgrade_speed").onclick = function() {
var upgrade_cost = (Math.pow(3, upgrade_speed)) * 100;
if (!buy_something(upgrade_cost, this)) return;
upgrade_speed++;
click_rate = click_rate * 0.90;
update_workers();
var e2 = document.getElementById("upgrade_speed");
e2.innerHTML = 'Buy for ' + ((Math.pow(3, upgrade_speed)) * 100);
var e2 = document.getElementById("speed_level");
e2.innerHTML = 'lvl ' + upgrade_speed;
};
//Start Autoclickers
update_workers();
function set_cookie(cookie_name, value) {
expiry = new Date();
expiry.setTime(new Date().getTime() + (10 * 60 * 1000));
var c_value = escape(btoa(JSON.stringify(value))) + "; expires=" + expiry.toUTCString();
document.cookie = cookie_name + "=" + c_value;
}
function get_cookie(cookie_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + cookie_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(cookie_name + "=");
}
if (c_start == -1) return false;
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = atob(unescape(c_value.substring(c_start, c_end)));
return JSON.parse(c_value);
}
</script>
我知道这真的很长,对不起.
I know this is really long, sorryyyy.
谢谢,彼得
不是您的代码无法正常工作,问题出在计算机上的浮点算术精度上. 0.05 + 0.01
的示例是众所周知的,例如 0.1 + 0.2
.您可以直接在控制台中尝试它,它会执行相同的操作.
It is not that your code is not working, the problem lies with floating point arithmetic precision on computers. The example of 0.05 + 0.01
is well known, as for 0.1+0.2
. You can try it directly in the console, it will do the same.
要了解更多信息,请查看为什么在Javascript中添加两个小数会产生错误的结果?.该资源特别擅长简单地说明问题: https://floating-point-gui.de/一个>.简短摘要:
To know more about this, have a look at Why does adding two decimals in Javascript produce a wrong result?. This resource is especially good at explaining simply the matter: https://floating-point-gui.de/. Short summary:
在内部,计算机使用的格式(二进制浮点数)根本无法准确表示一个数字,例如0.1、0.2或0.3.
Internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.
在编译或解释代码时,"0.1"已经四舍五入为该格式的最接近的数字,即使在计算之前,也会产生小的四舍五入误差.
When the code is compiled or interpreted, your "0.1" is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.
对于您的特定问题,可以使用 toPrecision
或 toFixed
舍入 clicks
变量并删除多余的数字.
For your specific problem, you can use toPrecision
or toFixed
to round the clicks
variable and remove extra digits.