久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Java 中舍入整數(shù)除法并獲得 int 結(jié)果?

How to round up integer division and have int result in Java?(如何在 Java 中舍入整數(shù)除法并獲得 int 結(jié)果?)
本文介紹了如何在 Java 中舍入整數(shù)除法并獲得 int 結(jié)果?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我剛剛寫了一個(gè)小方法來計(jì)算手機(jī)短信的頁(yè)數(shù).我沒有選擇使用 Math.ceil 進(jìn)行四舍五入,老實(shí)說,它看起來很丑.

I just wrote a tiny method to count the number of pages for cell phone SMS. I didn't have the option to round up using Math.ceil, and honestly it seems to be very ugly.

這是我的代碼:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   String message = "today we stumbled upon a huge performance leak while optimizing a raycasting algorithm. Much to our surprise, the Math.floor() method took almost half of the calculation time: 3 floor operations took the same amount of time as one trilinear interpolation. Since we could not belive that the floor-method could produce such a enourmous overhead, we wrote a small test program that reproduce";

   System.out.printf("COunt is %d ",(int)messagePageCount(message));



}

public static double messagePageCount(String message){
    if(message.trim().isEmpty() || message.trim().length() == 0){
        return 0;
    } else{
        if(message.length() <= 160){
            return 1;
        } else {
            return Math.ceil((double)message.length()/153);
        }
    }
}

我不太喜歡這段代碼,我正在尋找一種更優(yōu)雅的方式來做這件事.有了這個(gè),我期待 3 而不是 3.0000000.有什么想法嗎?

I don't really like this piece of code and I'm looking for a more elegant way of doing this. With this, I'm expecting 3 and not 3.0000000. Any ideas?

推薦答案

你可以使用整數(shù)除法來四舍五入

To round up an integer division you can use

import static java.lang.Math.abs;

public static long roundUp(long num, long divisor) {
    int sign = (num > 0 ? 1 : -1) * (divisor > 0 ? 1 : -1);
    return sign * (abs(num) + abs(divisor) - 1) / abs(divisor);
}

或者如果兩個(gè)數(shù)字都是正數(shù)

or if both numbers are positive

public static long roundUp(long num, long divisor) {
    return (num + divisor - 1) / divisor;
}

這篇關(guān)于如何在 Java 中舍入整數(shù)除法并獲得 int 結(jié)果?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 欧美日韩一区二区电影 | 成人亚洲精品久久久久软件 | 国产九九九九 | 成人在线视频免费看 | 日日操夜夜操视频 | 亚洲天堂精品一区 | 999久久 | 欧美日韩视频网站 | 黄色网毛片 | 欧美国产日韩一区二区三区 | 国产成人jvid在线播放 | 亚洲欧美日韩成人在线 | www.三级| 日韩免费福利视频 | 在线播放中文字幕 | 中文字幕高清av | 91在线视频免费观看 | 亚洲a在线观看 | 999久久久 | 99国产精品久久久 | 精品国产伦一区二区三区观看体验 | 亚洲人成在线观看 | 91福利在线观看视频 | 国产日产欧产精品精品推荐蛮挑 | 日韩成人av在线 | 国产一区精品在线 | 一区二区三区四区在线播放 | 国产成人一区二区三区久久久 | 免费视频一区二区 | 九九热在线视频 | 亚洲成年人免费网站 | 激情91| 国产 日韩 欧美 在线 | 殴美成人在线视频 | 国产日韩欧美 | 欧美日韩中文字幕 | 午夜成人免费视频 | 久久久久久亚洲精品 | 99re热精品视频国产免费 | 蜜桃视频在线观看免费视频网站www | 久久精品亚洲 |