如何显示正确的百分比? [英] How to show the right percentage?

查看:97
本文介绍了如何显示正确的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小提琴: https://jsfiddle.net/252jxsjq/1/



如何显示65而不是64.72343432 ...?



最后,如何让所有点击次数相等用户?
例如,现在如果我点击了60次按钮,如果我更改了pc,则显示的时间为0。 b
$ b

  var counter = localStorage.getItem('rans')|| 0; 
$ b $('。redanswer')。click(function(){
localStorage.setItem('rans',++ counter);
$('.bpercent') .html(counter1 * 100 /(counter1 + counter)+%);
$('.rpercent').html(counter * 100 /(counter1 + counter)+%);

});

var counter1 = localStorage.getItem('bans')|| 0; (bans',++ counter1);
('.rpercent')。 .text(counter * 100 /(counter1 + counter)+%);
$('.bpercent').text(counter1 * 100 /(counter1 + counter)+%);
});


解决方案

使用 Math.round JavaScript的功能。
$ b $($。$ $ $ $ $ $''redanswer')。click(function(){
localStorage.setItem('rans',++ counter);
$ ('.bpercent').html(Math.round(counter1 * 100 /(counter1 + counter))+%);
$('.rpercent').html(Math.round(counter * 100 /(counter1 + counter))+%);
});

本地存储将状态或变量保存到客户机(或称浏览器)。
正如Mike所说,如果您想为所有设备节省价值,那么您需要一些服务器端实现,以便为全球所有用户管理点击计数器。


fiddle: https://jsfiddle.net/252jxsjq/1/

how can I show 65 instead of 64.72343432...?

Finally,how can I make the number of clicks equal for all the user? For example, now if i click 60 times on a button, if I change the pc the times displayed are 0.

jQuery:

var counter = localStorage.getItem('rans') || 0;

$('.redanswer').click(function(){
     localStorage.setItem('rans', ++counter);
     $( '.bpercent' ).html( counter1 * 100 / (counter1+counter) + "%" );
     $( '.rpercent' ).html( counter * 100 / (counter1+counter) + "%" );

 });

var counter1 = localStorage.getItem('bans') || 0;

$('.blueanswer').click(function(){
     localStorage.setItem('bans', ++counter1);
     $( '.rpercent' ).text( counter * 100 / (counter1+counter) + "%" );
     $( '.bpercent' ).text( counter1 * 100 / (counter1+counter) + "%" );
});

解决方案

Use Math.round Function of JavaScript.

$('.redanswer').click(function(){
 localStorage.setItem('rans', ++counter);
 $( '.bpercent' ).html( Math.round( counter1 * 100 / (counter1+counter) ) + "%" );
 $( '.rpercent' ).html( Math.round( counter * 100 / (counter1+counter) ) + "%" );
});

Local storage saves state or variable to client machine (or say browser). As Mike said, If you want to save value for all devices, than You will need some server side Implementations which can manage clicks counter for all users all around the world.

这篇关于如何显示正确的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆