[英]Rounding up a number to nearest multiple of 5
有誰知道如何將一個數字四舍五入到最接近的 5 的倍數? 我找到了一種算法,可以將它四舍五入到最接近的 10 倍數,但我找不到這個。
這樣做十個。
double number = Math.round((len + 5)/ 10.0) * 10.0;
四舍五入到最接近的任何值
int round(double i, int v){
return Math.round(i/v) * v;
}
您還可以將Math.round()
替換為Math.floor()
或Math.ceil()
以使其始終向下舍入或始終向上舍入。
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
int roundUp(int num) {
return (int) (Math.ceil(num / 5d) * 5);
}
int round(int num) {
int temp = num%5;
if (temp<3)
return num-temp;
else
return num+5-temp;
}
int roundUp(int num) {
return ((num / 5) + (num % 5 > 0 ? 1 : 0)) * 5;
}
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;
}
}
這個 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
帶有擴展功能的 Kotlin。
可能在play.kotlinlang.org上運行
import kotlin.math.roundToLong
fun Float.roundTo(roundToNearest: Float): Float = (this / roundToNearest).roundToLong() * roundToNearest
fun main() {
println(1.02F.roundTo(1F)) // 1.0
println(1.9F.roundTo(1F)) // 2.0
println(1.5F.roundTo(1F)) // 2.0
println(1.02F.roundTo(0.5F)) // 1.0
println(1.19F.roundTo(0.5F)) // 1.0
println(1.6F.roundTo(0.5F)) // 1.5
println(1.02F.roundTo(0.1F)) // 1.0
println(1.19F.roundTo(0.1F)) // 1.2
println(1.51F.roundTo(0.1F)) // 1.5
}
可以像這樣使用 floor/ceil: fun Float.floorTo(roundToNearest: Float): Float = floor(this / roundToNearest) * roundToNearest
有些人在說類似
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);
輸出 :
Rounding up to nearest 5------20.0
我創建了一種方法,可以將一個數字轉換為將要傳入的最接近的數字,也許它會對某人有所幫助,因為我在這里看到了很多方法,但它對我沒有用,但這個方法可以:
/**
* 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;
}
來源: https ://www.geeksforgeeks.org/round-the-given-number-to-nearest-multiple-of-10/
Praveen Kumars 在此線程的其他地方提出問題
“為什么我們要在數字上加 4?”
非常相關。 這就是為什么我更喜歡這樣編碼:
int roundUpToMultipleOf5(final int n) {
return (n + 5 - 1) / 5 * 5;
}
或者,將值作為參數傳遞:
int roundUpToMultiple(final int n, final int multipleOf) {
return (n + multipleOf - 1) / multipleOf * multipleOf;
}
通過比您要查找的倍數少加 1,您已經添加了足夠多的值以確保n
的精確倍數不會四舍五入,並且n
的任何不是精確倍數的值都將是四舍五入到下一個倍數。
只需將您的數字作為雙精度數傳遞給此函數,它將返回您將十進制值向上舍入到最接近的值 5;
如果為 4.25,則輸出 4.25
如果是 4.20,輸出 4.20
如果是 4.24,輸出 4.20
如果為 4.26,則輸出 4.30
如果要四舍五入到小數點后 2 位,請使用
DecimalFormat df = new DecimalFormat("#.##");
roundToMultipleOfFive(Double.valueOf(df.format(number)));
如果最多 3 個位置,則 new DecimalFormat("#.###")
如果最多 n 個位置,則 new DecimalFormat("#. nTimes # ")
public double roundToMultipleOfFive(double x)
{
x=input.nextDouble();
String str=String.valueOf(x);
int pos=0;
for(int i=0;i<str.length();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))));
}
這是我用於四舍五入為數字的倍數的方法:
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
是我做的向上或向下舍入的枚舉。
祝你好運!
將給定數字四舍五入到最接近的 5 的倍數。
public static int round(int n)
while (n % 5 != 0) n++;
return n;
}
您可以使用此方法Math.round(38/5) * 5
獲得 5 的倍數
它可以根據您希望如何四舍五入的數字替換為Math.ceil
或Math.floor
使用此方法獲得最接近 5 的倍數。
private int giveNearestMul5(int givenValue){
int roundedNum = 0;
int prevMul5, nextMul5;
prevMul5 = givenValue - givenValue%5;
nextMul5 = prevMul5 + 5;
if ((givenValue%5!=0)){
if ( (givenValue-prevMul5) < (nextMul5-givenValue) ){
roundedNum = prevMul5;
} else {
roundedNum = nextMul5;
}
} else{
roundedNum = givenValue;
}
return roundedNum;
}
遞歸:
public static int round(int n){
return (n%5==0) ? n : round(++n);
}
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.