Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding in Swift with round()

While playing around, I found the round() function in swift. It can be used as below:

round(0.8) 

Which will return 1, as expected. Here's my question:

how do I round by thousandths in swift?

I want to be able to plug in a number, say 0.6849, and get 0.685 back. How does round() do this? Or, does it not, in which case, what function does?

like image 970
rocket101 Avatar asked Aug 26 '14 19:08

rocket101


People also ask

Does round () round up or down?

Rules for Rounding Here's the general rule for rounding: If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up. Example: 38 rounded to the nearest ten is 40. If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down.

How do you round to the nearest whole number in Swift?

To round a double to the nearest integer, just use round() .

What is round () function?

The ROUND function rounds a number to a specified number of digits. For example, if cell A1 contains 23.7825, and you want to round that value to two decimal places, you can use the following formula: =ROUND(A1, 2) The result of this function is 23.78.


1 Answers

You can do:

round(1000 * x) / 1000 
like image 127
Clément Bisaillon Avatar answered Sep 17 '22 18:09

Clément Bisaillon