获取一个值在两个值之间的百分比
Get the percentage of a value between two values
我有一个最小值和一个最大值(值可以改变)。
我也有一个介于这两个值之间的值。
private int minValue = 201;
private int maxValue = 500;
private int currentValue = 482
现在我需要获取 currentValue 在 0% 到 100% 之间的百分比。
这可能吗?
感谢您的帮助!
我想你想要的是这个计算:
double percentage = (currentValue - minValue) / (maxValue - minValue);
就像@kevin-esche 在评论中写的那样。
如果minValue为201,maxValue为500,则满量程为500 - 201 = 299,则如果currentValue为482:
((currentValue - minValue) / (maxValue - minValue)) * 100
=((482 - 201) / (500 - 201)) * 100
=(281 / 299) * 100
=93.97993
我有一个最小值和一个最大值(值可以改变)。 我也有一个介于这两个值之间的值。
private int minValue = 201;
private int maxValue = 500;
private int currentValue = 482
现在我需要获取 currentValue 在 0% 到 100% 之间的百分比。 这可能吗?
感谢您的帮助!
我想你想要的是这个计算:
double percentage = (currentValue - minValue) / (maxValue - minValue);
就像@kevin-esche 在评论中写的那样。
如果minValue为201,maxValue为500,则满量程为500 - 201 = 299,则如果currentValue为482:
((currentValue - minValue) / (maxValue - minValue)) * 100
=((482 - 201) / (500 - 201)) * 100
=(281 / 299) * 100
=93.97993