Java 将数字四舍五入到5的最接近倍数
有人知道如何将一个数字四舍五入到最接近的5的倍数吗?我找到了一个算法,把它四舍五入到10的最接近倍数,但我找不到这个 这个十美元Java 将数字四舍五入到5的最接近倍数,java,rounding,Java,Rounding,有人知道如何将一个数字四舍五入到最接近的5的倍数吗?我找到了一个算法,把它四舍五入到10的最接近倍数,但我找不到这个 这个十美元 double number = Math.round((len + 5)/ 10.0) * 10.0; int轮(int-num){ int temp=num%5; 如果(临时 注意-扬基威士忌的答案是四舍五入到最接近的倍数,这是四舍五入。如果需要它来处理负数,则需要进行修改。请注意,整数除法和相同数字的整数乘法是四舍五入的方法。多亏了阿米尔,我想我有了它 doub
double number = Math.round((len + 5)/ 10.0) * 10.0;
int轮(int-num){
int temp=num%5;
如果(临时
注意-扬基威士忌的答案是四舍五入到最接近的倍数,这是四舍五入。如果需要它来处理负数,则需要进行修改。请注意,整数除法和相同数字的整数乘法是四舍五入的方法。多亏了阿米尔,我想我有了它
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
递归:
public static int round(int n){
return (n%5==0) ? n : round(++n);
}
四舍五入到任意值的最近值
int round(double i, int v){
return Math.round(i/v) * v;
}
您还可以将Math.round()
替换为Math.floor()
或Math.ceil()
,使其始终向下取整或向上取整。代码:
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;
}
公共课数学
{
公共静态void main(字符串[]args){
运行测试();
}
公共静态双myFloor(双数值,双倍数){
返回值(数学下限(num/multipleOf)*multipleOf);
}
公共静态双线程(双num,双multipleOf){
返回值(Math.ceil(num/multipleOf)*multipleOf);
}
私有静态void运行测试(){
System.out.println(“myFloor(57.3,0.1):”+myFloor(57.3,0.1));
System.out.println(“mycil(57.3,0.1):”+mycil(57.3,0.1));
System.out.println(“”);
System.out.println(“myFloor(57.3,1.0):”+myFloor(57.3,1.0));
System.out.println(“mycil(57.3,1.0):”+mycil(57.3,1.0));
System.out.println(“”);
System.out.println(“myFloor(57.3,5.0):”+myFloor(57.3,5.0));
System.out.println(“mycil(57.3,5.0):”+mycil(57.3,5.0));
System.out.println(“”);
System.out.println(“myFloor(57.3,10.0):”+myFloor(57.3,10.0));
System.out.println(“mycil(57.3,10.0):”+mycil(57.3,10.0));
}
}
输出:菌丝体中也有一个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
我的楼层(57.3,0.1):57.2
菌丝体(57.3,0.1):57.3000000000000004
myFloor(57.3,1.0):57.0
菌丝体(57.3,1.0):58.0
myFloor(57.3,5.0):55.0
菌丝体(57.3,5.0):60.0
我的楼层(57.3,10.0):50.0
菌丝体(57.3,10.0):60.0
只需将您的数字作为双精度传递给此函数,它将返回您将十进制值四舍五入到最接近的值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个位置,则使用新的十进制格式(“#.###”)
如果最多有n个位置,则使用新的十进制格式(“#.nTimes”)
public double roundtomultipleof五(双x)
{
x=输入.nextDouble();
String str=String.valueOf(x);
int pos=0;
对于(int i=0;i,以下是我用于舍入到数字倍数的方法:
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
是向上或向下舍入的枚举
祝你好运!有些人说
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;
将数字四舍五入到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);
将给定数字四舍五入到5的最接近倍数
public static int round(int n)
while (n % 5 != 0) n++;
return n;
}
我创建了一个方法,可以将一个数字转换为将要传入的最接近的数字,也许这会对某些人有所帮助,因为我在这里看到了很多方法,但它对我不起作用,但这一个:
/**
* 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-(63->65)| 10-(124->120)。
*@param num-要舍入的数字
*@param nextest-要舍入到的最接近的数字(如果最接近的是5->(0-2.49将向下舍入)| |(2.5-4.99将向上舍入))
*@return Double-四舍五入的数字
*/
专用双循环(双数值,整数最接近){
如果(最近数百分比>=最近数/2){
num=num+((num%最近-最近)*-1);
}else if(最近数百分比<最近数/2){
num=num-(num%最近);
}
返回num;
}
如果只需对整数进行四舍五入,则可以使用此功能:
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));
}
}
}
公共静态长roundTo(长值,长roundTo){
如果(四舍五入到0){
返回值+(roundTo-Math.abs(余数));
}否则{
返回值-(roundTo-Math.abs(余数));
}
}
}
优点是它使用整数算术,甚至适用于浮点除法会给您带来问题的大长数
int roundUp(int n, int multipleOf)
{
int a = (n / multipleOf) * multipleOf;
int b = a + multipleOf;
return (n - a > b - n)? b : a;
}
来源:int-roundToNearestMultiple(int-num,int-multipleOf){
int floorNearest=((int)数学地板(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);
如果(floorNearestDiff此Kotlin函数将给定值“x”舍入为“n”的最接近倍数
fun roundXN(x: Long, n: Long): Long {
require(n > 0) { "n(${n}) is not greater than 0."}
return if (x >= 0)
((x + (n / 2.0)) / n).toLong() * n
else
((x - (n / 2.0)) / n).toLong() * n
}
fun main() {
println(roundXN(121,4))
}
输出:120您可以使用此方法Math.round(38/5)*5
获得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);
它可以是r
Rounding up to nearest 5------20.0
public static int round(int n)
while (n % 5 != 0) n++;
return n;
}
/**
* 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;
}
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;
}
int getNextMultiple(int num , int multipleOf) {
int nextDiff = multipleOf - (num % multipleOf);
int total = num + nextDiff;
return total;
}
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;
}
}
fun roundXN(x: Long, n: Long): Long {
require(n > 0) { "n(${n}) is not greater than 0."}
return if (x >= 0)
((x + (n / 2.0)) / n).toLong() * n
else
((x - (n / 2.0)) / n).toLong() * n
}
fun main() {
println(roundXN(121,4))
}