获取两个值之间的值的百分比

时间:2016-10-27 13:59:48

标签: java max min percentage

我有最小值和最大值(值可以更改)。 我还有一个介于这两个值之间的值。

private int minValue = 201;
private int maxValue = 500;
private int currentValue = 482

现在我需要将currentValue的百分比在0%和100%之间。 这甚至可能吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

我认为你想要的是这个计算:

double percentage = (currentValue - minValue) / (maxValue - minValue);

就像@ kevin-esche在评论中写道一样。

答案 1 :(得分:0)

如果minValue为201且maxValue为500,则整个范围为500 - 201 = 299,如果currentValue为482:

    ((currentValue - minValue) / (maxValue - minValue)) * 100
    =((482 - 201) / (500 - 201)) * 100
    =(281 / 299) * 100
    =93.97993
相关问题