
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift Double - remainder() Function
The remainder() method of Double structure calculates the remainder of the given value divided by the specified value. The remainder is the value that is left over when the given number is not completely divisible by another number. For example, we are dividing the numbers: 25 by 6, here 25 is not completely divisible by 6 hence 1 remainder is left.
Syntax
Following is the syntax of the remainder() method −
func remainder(dividingBy value: Self) ->Self
Parameter
This method takes only one parameter which is value. The value represents the divisor number.
Return Value
This method returns the remainder of the division.
Example 1
Swift program to demonstrate how to use the remainder() method.
import Foundation // Input numbers var number1 : Double = 8.0 var number2 : Double = 4.0 var number3 : Double = 45.0 // Finding remainder using remainder() method print("Remainder:", number1.remainder(dividingBy: 2.0)) print("Remainder:", number2.remainder(dividingBy: 3.1)) print("Remainder:", number3.remainder(dividingBy: 19.1))
Output
Remainder: 0.0 Remainder: 0.8999999999999999 Remainder: 6.799999999999997
Example 2
Swift program to calculate the remainder.
import Foundation // Finding remainder using remainder() method print("Remainder:", 18.0.remainder(dividingBy: 2.0)) print("Remainder:", 27.0.remainder(dividingBy: 3.0)) print("Remainder:", 134.6.remainder(dividingBy: 19.1))
Output
Remainder: 0.0 Remainder: 0.0 Remainder: 0.8999999999999844
Example 3
Swift program to calculate the remainder of a large number using the remainder() function.
import Foundation // Define a large dividend and divisor let largeDividend: Double = 4347889388347849.0 let divisor: Double = 78436986.0 // Finding the remainder using remainder() method print("Remainder left after dividing \(largeDividend) by \(divisor):", largeDividend.remainder(dividingBy: divisor))
Output
Remainder left after dividing 4347889388347849.0 by 78436986.0: 29576557.0
Advertisements