如何在Java中四舍五入到2.5?

huangapple go评论86阅读模式
英文:

How to round to 2.5 in Java?

问题

所以我正在制作一个适用于安卓的健身应用,现在我要求用户输入一个数字,例如72.5。

我会取这个数字的百分比,并对其应用函数等等。

我需要确保我取得的这个数字的百分比是以2.5为单位四舍五入的。这是因为在英国的健身房里,你只有以下这些重量片:1.25x2=2.5,2.5x2=5,5+2.5=7.5,10,15,20,25。

我的意思是,会有这些数字:40,42.5,45,47.5,50。

如何将一个数字N四舍五入到最近的2.5?我知道math.Round()会四舍五入到最近的整数,但是对于这样的自定义数字该怎么办呢?

英文:

So I am making a fitness app for android and now I am asking the user to input a number e.g. 72.5

I would take this number and take percentages of it and apply functions to this etc.

I need to make sure that the percentage I take of that number is rounded to 2.5. This is because in a UK gym you only have the following plates: 1.25x2=2.5 2.5x2=5 5+2.5=7.5 , 10, 15, 20, 25

What I mean is that it would be numbers like these: 40, 42.5, 45, 47.5, 50

How can I round a Number N to the nearest 2.5? I understand that math.Round() rounds to nearest whole but what about to a custom number like this?

答案1

得分: 1

按照以下方式执行:

public class Main {
    public static void main(String args[]) {
        // Tests
        System.out.println(roundToNearest2Point5(12));
        System.out.println(roundToNearest2Point5(14));
        System.out.println(roundToNearest2Point5(13));
        System.out.println(roundToNearest2Point5(11));
    }

    static double roundToNearest2Point5(double n) {
        return Math.round(n * 0.4) / 0.4;
    }
}

输出:

12.5
15.0
12.5
10.0

解释:

通过以下示例更容易理解:

double n = 20 / 3.0;
System.out.println(n);
System.out.println(Math.round(n));
System.out.println(Math.round(n * 100.0));
System.out.println(Math.round(n * 100.0) / 100.0);

输出:

6.666666666666667
7
667
6.67

正如您在此处看到的,四舍五入 20 / 3.0 返回 7(这是在将 0.5 加到 20 / 3.0 后的底值。请查看此处了解实现方式)。然而,如果您想将其四舍五入到最接近的 1/100 位置(即到小数点后 2 位),一种更简单的方法(但不太精确。查看此处以获取更精确的方法)是将 n * 100.0 四舍五入(这将使其变为 667),然后除以 100.0,这将得到 6.67(即到小数点后 2 位)。请注意,1 / (1 / 100.0) = 100.0

同样,要将数字四舍五入到最接近的 2.5 位置,您需要使用 1 / 2.5 = 0.4,即 Math.round(n * 0.4) / 0.4

要将数字四舍五入到最接近的 100 位置,您需要使用 1 / 100 = 0.01,即 Math.round(n * 0.1) / 0.1

要将数字四舍五入到最接近的 0.5 位置,您需要使用 1 / 0.5 = 2.0,即 Math.round(n * 2.0) / 2.0

我希望这样清楚了解了这个过程。

英文:

Do it as follows:

public class Main {
	public static void main(String args[]) {
		// Tests
		System.out.println(roundToNearest2Point5(12));
		System.out.println(roundToNearest2Point5(14));
		System.out.println(roundToNearest2Point5(13));
		System.out.println(roundToNearest2Point5(11));
	}

	static double roundToNearest2Point5(double n) {
		return Math.round(n * 0.4) / 0.4;
	}
}

Output:

12.5
15.0
12.5
10.0

Explanation:

It will be easier to understand with the following example:

double n = 20 / 3.0;
System.out.println(n);
System.out.println(Math.round(n));
System.out.println(Math.round(n * 100.0));
System.out.println(Math.round(n * 100.0) / 100.0);

Output:

6.666666666666667
7
667
6.67

As you can see here, rounding 20 / 3.0 returns 7 (which is the floor value after adding 0.5 to 20 / 3.0. Check this to understand the implementation). However, if you wanted to round it up to the nearest 1/100th place (i.e. up to 2 decimal places), an easier way (but not so precise. Check this for more precise way) would be to round n * 100.0 (which would make it 667) and then divide it by 100.0 which would give 6.67 (i.e. up to 2 decimal places). Note that 1 / (1 / 100.0) = 100.0

Similarly, to round the number to the nearest 2.5th place, you will need to do the same thing with 1 / 2.5 = 0.4 i.e. Math.round(n * 0.4) / 0.4.

To round a number to the nearest 100th place, you will need to do the same thing with 1 / 100 = 0.01 i.e. Math.round(n * 0.1) / 0.1.

To round a number to the nearest 0.5th place, you will need to do the same thing with 1 / 0.5 = 2.0 i.e. Math.round(n * 2.0) / 2.0.

I hope, it is clear.

答案2

得分: 1

晚了回复,但你也可以这样做:2.5 *(Math.round(number/2.5))
同样的方法,如果你想要将磅数四舍五入到最近的5,可以用这个公式:5 *(Math.round(number/5))

英文:

Late reply but you can also do 2.5*(Math.round(number/2.5))
Same way if you wanted to round in lbs to the nearest 5th its 5*(Math.round(number/5))

huangapple
  • 本文由 发表于 2020年3月17日 03:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/60712200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定