Java5的倍数_关于java:将数字四舍五入到最接近的5的倍数

有谁知道如何将数字四舍五入到最接近的5的倍数? 我找到了一种算法将其四舍五入到最接近的10的倍数,但找不到此算法。

这样做十个。

double number = Math.round((len + 5)/ 10.0) * 10.0;

您的10位代码是什么样的? 将10更改为5应该相当简单。

len的类型是什么? 是int还是double?

它的两倍。 如果我没有将其初始化为双精度,它将给我一个错误,我认为

double number = Math.round((len + 2.5) 5.0) * 5.0; ??

如何始终舍入到下一个整数的可能重复项

@bestsss问题不是说要舍入整数,不是吗?

@JuanMendes确切地说,问题字面意思是double,并使用了10.0

四舍五入到任何值的最接近值

int round(double i, int v){

return Math.round(i/v) * v;

}

您也可以将Math.round()替换为Math.floor()或Math.ceil(),以使其始终向下舍入或始终向上舍入。

到目前为止,这是最好的答案。它是通用的,它将可以四舍五入到任何整数(例如2或5)或任何分数(例如0.1或0.05)中最接近的整数。

但是必须对其进行编辑以采用双精度并返回双精度以处理小数

我不确定为什么它实际上没有编译时为什么会有这么多的选票

@ dave.c确实可以编译。

@SamJakob当我使用Java 1.8.0_152进行编译时,得到error: incompatible types: possible lossy conversion from long to int。为了使其能够编译,您需要强制转换Math.round的结果或将返回类型更改为long。

@ dave.c我的错-我没有注意到,我只在返回双精度值的函数中使用了它。

提供正确答案的道具。使用不可读变量名的零道具(双精度i,双精度v)。

挑剔:如何舍入到最近的" Foo"?不应该说"任何值",也许是"任何整数值"?

int roundUp(int n) {

return (n + 4) / 5 * 5;

}

注意-YankeeWhiskey的答案四舍五入到最接近的倍数,四舍五入。如果需要用于负数,则需要进行修改。请注意,整数除法后跟相同数字的整数乘法是四舍五入的方法。

我想我有了,多亏了Amir

double round( double num, int multipleOf) {

return Math.floor((num + multipleOf/2) / multipleOf) * multipleOf;

}

这是我运行的代码

class Round {

public static void main(String[] args){

System.out.println("3.5 round to 5:" + Round.round(3.5, 5));

System.out.println("12 round to 6:" + Round.round(12, 6));

System.out.println("11 round to 7:"+ Round.round(11, 7));

System.out.println("5 round to 2:" + Round.round(5, 2));

System.out.println("6.2 round to 2:" + Round.round(6.2, 2));

}

public static double round(double num, int multipleOf) {

return Math.floor((num +  (double)multipleOf / 2) / multipleOf) * multipleOf;

}

}

这是输出

3.5 round to 5: 5.0

12 round to 6: 12.0

11 round to 7: 14.0

5 round to 2: 6.0

6.2 round to 2: 6.0

multipleOf是一个int,因此将其除以2可能会引起问题。

如果multipleOf == 5,则multipleOf / 2 == 2

@AmirPashazadeh:没错,我还需要使用地板,而不是圆形,因为我已经添加了multipleOf2。还有其他问题吗?

int roundUp(int num) {

return (int) (Math.ceil(num / 5d) * 5);

}

用我的示例对此进行了测试,并且可以正常工作。即使是任何倍数public static int roundUp(double num, int multipleOf) { return (int) (Math.ceil(num (double)multipleOf) * multipleOf); }

int roundUp(int num) {

return ((num / 5) + (num % 5 > 0 ? 1 : 0)) * 5;

}

int round(int num) {

int temp = num%5;

if (temp<3)

return num-temp;

else

return num+5-temp;

}

int getNextMultiple(int num , int multipleOf) {

int nextDiff = multipleOf - (num % multipleOf);

int total = num + nextDiff;

return total;

}

我创建了一种方法,该方法可以将数字转换为将要传递的最接近的数字,也许会对某人有所帮助,因为我在这里看到了很多方法,但对我来说不起作用,但是这个方法可以:

/**

* The method is rounding a number per the number and the nearest that will be passed in.

* If the nearest is 5 - (63->65) | 10 - (124->120).

* @param num - The number to round

* @param nearest - The nearest number to round to (If the nearest is 5 -> (0 - 2.49 will round down) || (2.5-4.99 will round up))

* @return Double - The rounded number

*/

private Double round (double num, int nearest) {

if (num % nearest >= nearest / 2) {

num = num + ((num % nearest - nearest) * -1);

} else if (num % nearest < nearest / 2) {

num = num - (num % nearest);

}

return num;

}

将数字四舍五入到最接近的5的倍数的另一种方法或逻辑

double num = 18.0;

if (num % 5 == 0)

System.out.println("No need to roundoff");

else if (num % 5 < 2.5)

num = num - num % 5;

else

num = num + (5 - num % 5);

System.out.println("Rounding up to nearest 5------" + num);

输出:

Rounding up to nearest 5------20.0

有人说

int n = [some number]

int rounded = (n + 5) / 5 * 5;

这将舍入为5到10,以及6、7、8和9(全部为10)。但是,您不希望5舍入到10。当仅处理整数时,您想将4加到n而不是5。因此,采用该代码并将5替换为4:

int n = [some number]

int rounded = (n + 4) / 5 * 5;

当然,在处理双精度数时,只需输入4.99999之类的值,或者如果您想考虑所有情况(如果您要处理更精确的双精度数),请添加条件语句:

int n = [some number]

int rounded = n % 5 == 0 ? n : (n + 4) / 5 * 5;

这是我用于四舍五入到数字的倍数的方法:

private int roundToMultipleOf(int current, int multipleOf, Direction direction){

if (current % multipleOf == 0){

return ((current / multipleOf) + (direction == Direction.UP ? 1 : -1)) * multipleOf;

}

return (direction == Direction.UP ? (int) Math.ceil((double) current / multipleOf) : (direction == Direction.DOWN ? (int) Math.floor((double) current / multipleOf) : current)) * multipleOf;

}

变量current是您要四舍五入的数字,multipleOf是您想要的倍数(即四舍五入到最接近的20,最接近的10等),而direction是我对任一舍入进行的枚举上或下。

祝好运!

递归:

public static int round(int n){

return (n%5==0) ? n : round(++n);

}

公式是(x + y-1)/ y,如果递归看起来很酷,则不知道,想象一下只有几千个-bam StackOverflow。

你抓到我了我经常在堆栈溢出中浮动,试图看起来很酷。这是另一种建议。有很多方法。

只需将您的数字作为双精度形式传递给此函数,它将返回您将十进制值四舍五入到最接近的值5;

如果是4.25,则输出4.25

如果是4.20,则输出4.20

如果是4.24,则输出4.20

如果是4.26,则输出4.30

如果您想将小数点后两位取整,请使用

DecimalFormat df = new DecimalFormat("#.##");

roundToMultipleOfFive(Double.valueOf(df.format(number)));

如果最多3个地方,则新的DecimalFormat("#。###")

如果最多n个地方,则新的DecimalFormat("#。nTimes#")

public double roundToMultipleOfFive(double x)

{

x=input.nextDouble();

String str=String.valueOf(x);

int pos=0;

for(int i=0;i

{

if(str.charAt(i)=='.')

{

pos=i;

break;

}

}

int after=Integer.parseInt(str.substring(pos+1,str.length()));

int Q=after/5;

int R =after%5;

if((Q%2)==0)

{

after=after-R;

}

else

{

after=after+(5-R);

}

return Double.parseDouble(str.substring(0,pos+1).concat(String.valueOf(after))));

}

int roundToNearestMultiple(int num, int multipleOf){

int floorNearest = ((int) Math.floor(num * 1.0/multipleOf)) * multipleOf;

int ceilNearest = ((int) Math.ceil(num  * 1.0/multipleOf)) * multipleOf;

int floorNearestDiff = Math.abs(floorNearest - num);

int ceilNearestDiff = Math.abs(ceilNearest - num);

if(floorNearestDiff <= ceilNearestDiff) {

return floorNearest;

} else {

return ceilNearest;

}

}

将给定数字四舍五入到最接近的5的倍数。

public static int round(int n)

while (n % 5 != 0) n++;

return n;

}

这是最糟糕的方法之一,因为它需要多次慢速分割

如果只需要四舍五入,可以使用以下函数:

public static long roundTo(long value, long roundTo) {

if (roundTo <= 0) {

throw new IllegalArgumentException("Parameter 'roundTo' must be larger than 0");

}

long remainder = value % roundTo;

if (Math.abs(remainder) < (roundTo / 2d)) {

return value - remainder;

} else {

if (value > 0) {

return value + (roundTo - Math.abs(remainder));

} else {

return value - (roundTo - Math.abs(remainder));

}

}

}

优点是它使用整数算术,并且即使对于浮点除法会引起问题的大整数也可以使用。

int roundUp(int n, int multipleOf)

{

int a = (n / multipleOf) * multipleOf;

int b = a + multipleOf;

return (n - a > b - n)? b : a;

}

来源:https://www.geeksforgeeks.org/round-the-given-number-to-nearest-multiple-of-10/

if (n % 5 == 1){

n -= 1;

} else if (n % 5 == 2) {

n -= 2;

} else if (n % 5 == 3) {

n += 2;

} else if (n % 5 == 4) {

n += 1;

}

码:

public class MyMath

{

public static void main(String[] args) {

runTests();

}

public static double myFloor(double num, double multipleOf) {

return ( Math.floor(num / multipleOf) * multipleOf );

}

public static double myCeil (double num, double multipleOf) {

return ( Math.ceil (num / multipleOf) * multipleOf );

}

private static void runTests() {

System.out.println("myFloor (57.3,  0.1) :" + myFloor(57.3, 0.1));

System.out.println("myCeil  (57.3,  0.1) :" + myCeil (57.3, 0.1));

System.out.println("");

System.out.println("myFloor (57.3,  1.0) :" + myFloor(57.3, 1.0));

System.out.println("myCeil  (57.3,  1.0) :" + myCeil (57.3, 1.0));

System.out.println("");

System.out.println("myFloor (57.3,  5.0) :" + myFloor(57.3, 5.0));

System.out.println("myCeil  (57.3,  5.0) :" + myCeil (57.3, 5.0));

System.out.println("");

System.out.println("myFloor (57.3, 10.0) :" + myFloor(57.3,10.0));

System.out.println("myCeil  (57.3, 10.0) :" + myCeil (57.3,10.0));

}

}

输出:myCeil中也存在一个错误,该错误也是0.1的倍数……不知道为什么。

myFloor (57.3,  0.1) : 57.2

myCeil  (57.3,  0.1) : 57.300000000000004

myFloor (57.3,  1.0) : 57.0

myCeil  (57.3,  1.0) : 58.0

myFloor (57.3,  5.0) : 55.0

myCeil  (57.3,  5.0) : 60.0

myFloor (57.3, 10.0) : 50.0

myCeil  (57.3, 10.0) : 60.0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值