Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有极性的Java百分比差异逻辑_Java - Fatal编程技术网

具有极性的Java百分比差异逻辑

具有极性的Java百分比差异逻辑,java,Java,我试图在两个值之间创建一个百分比差-我需要a)确保呈现的值是正确的-例如,如果它是+35%,或250%,-25%b)解密它是积极的还是消极的变化 --情景2 这一时期——20名成员 上期-15名成员 So there is a 33% difference. (133-100?) --情景3 这一时期——20名成员 上期-10名成员 So there is a +200% difference. -- but then shouldn't this be 100% difference (2

我试图在两个值之间创建一个百分比差-我需要a)确保呈现的值是正确的-例如,如果它是+35%,或250%,-25%b)解密它是积极的还是消极的变化

--情景2 这一时期——20名成员 上期-15名成员

So there is a 33% difference. (133-100?)
--情景3 这一时期——20名成员 上期-10名成员

So there is a +200% difference. -- but then shouldn't this be 100% difference (200-100?)
So there is a +400% difference. -- but then shouldn't this be 300% difference (400-100?)
--情景4 这一时期——40名成员 上期-10名成员

So there is a +200% difference. -- but then shouldn't this be 100% difference (200-100?)
So there is a +400% difference. -- but then shouldn't this be 300% difference (400-100?)

---我在这里的逻辑正确吗?你的逻辑似乎有点不正确。如果您想要相对于上一个值的百分比变化,我认为它应该是
差异/old_值

public static String percentageChange(double last, double now) {
    double delta = now - last;
    double change = delta / last;
    return String.format("%+.2f%%", change * 100);
}
示例:

percentageChange( 8, 10);    // +25.00%
percentageChange(15, 10);    // -33.33%
percentageChange(10, 30);    // +200.00%

你的逻辑似乎有点错误。如果您想要相对于上一个值的百分比变化,我认为它应该是
差异/old_值

public static String percentageChange(double last, double now) {
    double delta = now - last;
    double change = delta / last;
    return String.format("%+.2f%%", change * 100);
}
示例:

percentageChange( 8, 10);    // +25.00%
percentageChange(15, 10);    // -33.33%
percentageChange(10, 30);    // +200.00%

以上一期间百分比表示的当前期间将始终为

thisPeriod / lastPeriod * 100
因此,如果
thispiriod==lastpiriod
,则这是100,如果
thispiriod
lastpiriod
的一半,则得到50,依此类推

正如你似乎已经怀疑的那样,百分比变化少了100个百分点:

thisPeriod / lastPeriod * 100 - 100
我们在这里减去100%,因为我们想忽略上一阶段已经在那里的成员。这与将变化表示为上一期间的百分比相同:

(thisPeriod - lastPeriod) / lastPeriod * 100

您认为这两种公式中哪一种更具可读性取决于您。

当前期间表示为最后一个期间的百分比将始终为

thisPeriod / lastPeriod * 100
因此,如果
thispiriod==lastpiriod
,则这是100,如果
thispiriod
lastpiriod
的一半,则得到50,依此类推

正如你似乎已经怀疑的那样,百分比变化少了100个百分点:

thisPeriod / lastPeriod * 100 - 100
我们在这里减去100%,因为我们想忽略上一阶段已经在那里的成员。这与将变化表示为上一期间的百分比相同:

(thisPeriod - lastPeriod) / lastPeriod * 100

这两个公式中,你觉得哪一个更具可读性取决于你。

此外-我想我需要一种处理或防止无穷大值的方法-建议?为什么是15->20“-33%”?场景3和4:是,这些可能是+100%和+300%。@tobias_k--我的意思是--33%。@tobias_k--我想我需要一种处理或防止无穷大值的方法--建议?为什么是15->20”-33%?场景3和4:是的,这些可能是+100%和+300%。@tobias_k--我的意思是--33%