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

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

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

問題描述

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

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
}

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

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 庫中有一個(gè)round 可用(其實(shí)在Darwin,但是 Foundation 導(dǎo)入 Darwin 并且大多數(shù)時(shí)候你會(huì)想要使用 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)

在操場上運(yùn)行您的代碼,然后調(diào)用:

Running your code in a playground and then calling:

print(round(users))

輸出:

15.0

round() 總是在小數(shù)位為 >= .5 時(shí)向上舍入,在 時(shí)總是向下舍入..5(標(biāo)準(zhǔn)舍入).您可以使用 floor() 強(qiáng)制向下舍入,使用 ceil() 強(qiáng)制向上舍入.

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):

四舍五入到小數(shù)點(diǎn)后兩位:

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

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

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.

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

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

相關(guān)文檔推薦

Which is the best way to estimate measure of photographed things?(哪種方法是估計(jì)拍攝物體尺寸的最佳方法?)
Convert Character to Int in Swift 2.0(在 Swift 2.0 中將字符轉(zhuǎn)換為 Int)
Edit Text shows exception Invalid Int quot;quot;(編輯文本顯示異常 Invalid Int “;)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉(zhuǎn)換為字符)
In Swift 3, how to calculate the factorial when the result becomes too high?(在 Swift 3 中,當(dāng)結(jié)果變得太高時(shí)如何計(jì)算階乘?)
How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創(chuàng)建十六進(jìn)制顏色字符串 UIColor 初始化程序?)
主站蜘蛛池模板: 国产亚洲一区二区三区 | 成人一区av偷拍 | 国产99久久久国产精品下药 | 国产超碰人人爽人人做人人爱 | 在线观看免费高清av | 国产黄色大片网站 | jav成人av免费播放 | 国产精品九九九 | 激情国产视频 | www.久久 | 天天综合网7799精品 | 黄色av免费| 欧美激情网站 | 春色av| 亚洲高清在线免费观看 | 久久久高清 | 欧美性猛交 | 久久国产免费 | 欧美在线a | 亚洲综合色视频在线观看 | 一区二区三区av夏目彩春 | 欧美久久一区二区 | 中国av在线免费观看 | 精品久久精品 | 亚洲午夜网 | 性网站免费 | 亚洲二区视频 | 天天综合网天天综合 | 国产一区二区三区四区三区四 | 国产精品区二区三区日本 | 黄网站免费在线 | 久久久精品视频一区二区三区 | 天天精品在线 | 免费看a | 欧美二区三区 | 夜夜久久 | 午夜影视 | 精品欧美 | 日韩在线观看网站 | 日本一区二区在线视频 | 在线观看的av|