久久久久久久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 初始化程序?)
主站蜘蛛池模板: 国产精品污www一区二区三区 | 久久久久久亚洲精品 | 日韩一级黄色毛片 | 污书屋 | 国产午夜精品一区二区三区在线观看 | 91av在线免费看 | 日韩欧美一区二区三区四区 | 国产成人精品a视频一区www | 国产精品久久久久久久久久久免费看 | 欧美一级小视频 | 精产国产伦理一二三区 | 九九九国产 | 人人做人人澡人人爽欧美 | 人人干人人看 | 亚洲成人精品久久 | 国产线视频精品免费观看视频 | 97超碰免费 | 国产一级视频免费播放 | 亚洲高清av在线 | 亚洲国产成人精品久久久国产成人一区 | 日日干干 | 99爱视频 | 中文福利视频 | 亚洲成人精品久久久 | 欧美一区视频 | 久久国产日韩 | 99精品国产一区二区三区 | 国产一二区视频 | 美女在线视频一区二区三区 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 久久九七 | 成人午夜免费福利视频 | 久久久久国产一区二区三区四区 | 午夜精品一区二区三区在线观看 | 国产精品久久久久久久久久久久 | 黄视频网站在线 | 精品一区二区三区在线视频 | 成年人免费网站 | 午夜精品在线观看 | 日本精品视频在线观看 | 欧美日韩精品 |