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

如何快速將 Double 舍入到最近的 Int?

How to round a Double to the nearest Int in swift?(如何快速將 Double 舍入到最近的 Int?)
本文介紹了如何快速將 Double 舍入到最近的 Int?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試制作一個增長率計算器(Double),它將結果四舍五入到最接近的整數并從那里重新計算,如下所示:

I'm trying to make a calculator of growth rate (Double) that will round the result to the nearest Integer and recalculate from there, as such:

let firstUsers = 10.0
let growth = 0.1
var users = firstUsers
var week = 0


while users < 14 {
    println("week (week) has (users) users")
    users += users * growth
    week += 1
}

但到目前為止我一直做不到.

but I've been unable so far.

編輯我是這樣做的:

var firstUsers = 10.0
let growth = 0.1
var users:Int = Int(firstUsers)
var week = 0


while users <= 14 {
    println("week (week) has (users) users")
    firstUsers += firstUsers * growth
    users = Int(firstUsers)
    week += 1
}

雖然我不介意它總是四舍五入,但我不喜歡它,因為 firstUsers 必須成為變量并在整個程序中更改(以便進行下一次計算),我不希望它發生.

Although I don't mind that it is always rounding down, I don't like it because firstUsers had to become a variable and change throughout the program (in order to make the next calculation), which I don't want it to happen.

推薦答案

Foundation 庫中有一個round 可用(其實在Darwin,但是 Foundation 導入 Darwin 并且大多數時候你會想要使用 Foundation 而不是使用 Darwin 直接).

There is a round available in the Foundation library (it's actually in Darwin, but Foundation imports Darwin and most of the time you'll want to use Foundation instead of using Darwin directly).

import Foundation

users = round(users)

在操場上運行您的代碼,然后調用:

Running your code in a playground and then calling:

print(round(users))

輸出:

15.0

round() 總是在小數位為 >= .5 時向上舍入,在 時總是向下舍入..5(標準舍入).您可以使用 floor() 強制向下舍入,使用 ceil() 強制向上舍入.

round() always rounds up when the decimal place is >= .5 and down when it's < .5 (standard rounding). You can use floor() to force rounding down, and ceil() to force rounding up.

如果需要四舍五入到特定的地方,那么你乘以pow(10.0, number of places)round,再除以pow(10,名額):

If you need to round to a specific place, then you multiply by pow(10.0, number of places), round, and then divide by pow(10, number of places):

四舍五入到小數點后兩位:

Round to 2 decimal places:

let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
let num = 10.12345
let rounded = round(num * multiplier) / multiplier
print(rounded)

輸出:

10.12

注意:由于浮點數學的工作方式,rounded 可能并不總是完全準確.最好將其更多地考慮為舍入的近似值.如果您這樣做是為了顯示,最好使用字符串格式化來格式化數字,而不是使用數學來舍入它.

Note: Due to the way floating point math works, rounded may not always be perfectly accurate. It's best to think of it more of an approximation of rounding. If you're doing this for display purposes, it's better to use string formatting to format the number rather than using math to round it.

這篇關于如何快速將 Double 舍入到最近的 Int?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Which is the best way to estimate measure of photographed things?(哪種方法是估計拍攝物體尺寸的最佳方法?)
Convert Character to Int in Swift 2.0(在 Swift 2.0 中將字符轉換為 Int)
Edit Text shows exception Invalid Int quot;quot;(編輯文本顯示異常 Invalid Int “;)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉換為字符)
In Swift 3, how to calculate the factorial when the result becomes too high?(在 Swift 3 中,當結果變得太高時如何計算階乘?)
How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創建十六進制顏色字符串 UIColor 初始化程序?)
主站蜘蛛池模板: 激情丁香婷婷 | av黄色在线观看 | www.日韩av | 久久精品久久久久久久 | 亚洲经典一区 | 成人免费福利 | 日韩欧美视频在线 | 国产一区欧美 | 成人做爰免费视频免费看 | 91在线看片 | 户外少妇对白啪啪野战 | 国产精品国产精品国产专区不卡 | 亚洲综合久久久 | 久久手机视频 | 国产色在线| 日韩欧美视频一区 | 欧美国产日韩一区二区 | 亚洲激情一区二区 | 成人在线观看免费爱爱 | 肉丝美脚视频一区二区 | 亚洲激情文学 | 伊人9999 | 日韩视频免费 | 伊人网址| 天天摸夜夜操 | 免费a在线| 国内外成人免费视频 | 综合导航 | 在线观看国产免费视频 | 欧美日韩在线播放 | 麻豆精品一区 | 最新超碰| 国产91热爆ts人妖系列 | 欧美视频久久 | 国语对白做受69 | 亚洲欧美精品一区二区 | 午夜影院在线观看视频 | 日韩av免费在线播放 | 国产一区在线观看视频 | 在线观看亚洲 | 国产精品一区三区 |