Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift round double to 2 decimal places

You can use Swift's round function to accomplish this.

To round a Double with 3 digits precision, first multiply it by 1000, round it and divide the rounded result by 1000:

let x = 1.23556789
let y = Double(round(1000*x)/1000)
print(y)  // 1.236
Other than any kind of printf(...) or String(format: ...) solutions, the result of this operation is still of type Double.

EDIT:
Regarding the comments that it sometimes does not work, please read this:
Comment

PREVIOUS NEXT
Code Example
Swift :: button color swiftui 
Swift :: how to call app delegate function in swift 
Swift :: swift scroll to tableviewcell 
Swift :: an in swift 
Swift :: swift change background color 
Swift :: tuple swift 
Swift :: tableview cell animation swift 
Swift :: define enum swift 
Swift :: swift push view controller programmatically 
Swift :: swift sort array 
Swift :: add navigation bar button swiftui 
Swift :: enviroment dissmiss swiftui 
Swift :: swift pop view controller 
Swift :: swiftui coin flip 
Swift :: uitextview text alignment 
Swift :: record permission swift 4 
Swift :: reprobate 
Swift :: swift extension Array with element 
Swift :: Swift Loop Over Array 
Swift :: string to array swift 
Swift :: get middle index of center cell in table view swift 
Swift :: Swift Closure as function parameter 
Swift :: Swift static Methods 
Swift :: swift weekday component values 
Swift :: Swift Boolean Literals 
Swift :: func collectionview 
Swift :: Function Return Types in swift 
Swift :: protocol oriented programming 
Swift :: swift 5 cancel or end block operation 
Swift :: Convert struct to JSON string in swift 5 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =