[Swift5] How to round off, round down, and round up to the second or third decimal place

Functions you need

Use the following function to round, round, or round up.

round () → rounding fool () → truncate ceil () → Round up

Example of use

ViewController.swift


let num = 7.5
 
let numRound = round(num)   
//8 (rounded)

let numFloor = floor(num)   
//7 truncated)

let numCeil  = ceil(num)    
//8 (rounded up)

Rounding / rounding / rounding up to the second or third decimal place

ViewController.swift


let num = 3.1415
 
let numRound = round(num*10)/10   
// 3.1 (rounded to the first decimal place)

let numFloor = floor(num*100)/100   
// 3.14 (rounded down to the first decimal place)

let numCeil =ceil(num*1000)/1000   
// 3.142 (rounded up to 4 decimal places)

that's all. please refer!

Recommended Posts

[Swift5] How to round off, round down, and round up to the second or third decimal place
[Ruby] How to get the tens place and the ones place
[Swift5] How to get an array and the complement of arrays
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to place and share SwiftLint config files on the server
[Android] How to turn the Notification panel on and off using StatusBarManager
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to set up and use kapt
[Swift] How to implement the countdown function
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
[Swift] How to implement the fade-in / out function
How to find the total score and average score
A memo about the types of Java O/R mappers and how to select them
How to convert A to a and a to A using AND and OR in Java
Extension to add up / down button and finish button to UIToolBar
[Swift] How to get the document ID of Firebase
How to set up Android OR mapper "Orma" (Kotlin)
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
How to mark up search keywords regardless of case and without changing the search source word