简体   繁体   English

如何从javascript momentjs.asHours()获得每天的百分比以提高精度?

[英]how to get percentage a day to more precision from javascript momentjs.asHours()?

I have table like this: 我有这样的表:

在此处输入图片说明

The total of the Percentage column must be 100%, but I'm getting 100.01%. 百分比列的总和必须为100%,但我的分数为100.01%。

Here's my code: 这是我的代码:

var hourSlow = $("#input-me-slow").val();
var slowTime = moment.duration(hourSlow).asHours();
// slow time return 2.1666666666666665

var hourIdle = $("#input-me-idle").val();
var idleTime = moment.duration(hourIdle).asHours()
// idle time return 1

var hourEco = $("#input-me-eco").val();
var ecoTime = moment.duration(hourEco).asHours();
// ecoTime return 20.166666666666668

var hourSpeed = $("#input-me-speed").val();
var speedTime = moment.duration(hourSpeed).asHours();
// speedTime return 0.6666666666666666

var fTime = "24:00"
var dfTime = moment.duration(fTime).asHours();
// dfTime return 24

var totalTime = dfTime-speedTime-ecoTime-slowTime-idleTime;
// totalTime return -2.220446049250313e-15


// Here for display it to table, the problem is here
var fPercent = toHour(fullTime); //return 2.78
var ePercent = toHour(ecoTime); //return 84.03
var sPercent = toHour(slowTime); //return 9.03
var iPercent = toHour(idleTime); //return 4.17
$("#me_fullpercen").text(addCommas(fPercent));
$("#me_ecopercen").text(addCommas(ePercent));
$("#me_slowpercen").text(addCommas(sPercent));
$("#me_idlepercen").text(addCommas(iPercent));

// here the function of toHour (I dont know maybe the problem is here)
function toHour(num) {
    var result = (num / 24) * 100;
    return result ;
}

I would rather not round the percentage to 100%, as that would be less precise. 我宁愿不将百分比四舍五入为100%,因为这不太准确。

How can I make my percentage 100% instead of 100.01%? 如何将我的百分比从100.01%变为100%?

This is a common problem, and no matter how precise you try to be, the computer will need to round numbers with repeating decimals at some point. 这是一个普遍的问题,并且无论您尝试达到的精度如何,计算机都需要在某些时候用重复的小数位四舍五入。 Here's some posts that deal with it: 这是一些处理它的帖子:

In those posts you can read about many complex ways to get very close to 100%, but basically there is no right way to do this - when it's all boiled down it's still going to be an estimate - not exactly precise because we're dealing with not-precise numbers. 在这些文章中,您可以了解许多非常接近100%的复杂方法,但是基本上没有正确的方法可以做到这一点-当所有问题都归结到一起时,这仍将是一个估计-不太精确,因为我们正在处理数字不准确。 That's just the nature of the beast. 那只是野兽的本性。

Your program is going to round numbers the wrong way because it's a computer, and it's not intelligent. 您的程序将以错误的方式对数字进行四舍五入,因为它是一台计算机,并且不智能。

Depending on your application, you may want to invest time into reading how to do those complex methods, and maybe you'll get really close. 根据您的应用程序,您可能需要花费时间来阅读如何做那些复杂的方法,也许您会非常接近。

Add a Footnote 添加脚注

Any path you choose, you'll probably end up putting a footnote explaining this problem anyway. 无论选择哪种方式,您最终都会在脚注中说明该问题。 Something like this: 像这样:

*Because of rounding, these values may not add up to 100%. *由于四舍五入,这些值的总和可能不等于100%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM