Double
public extension Double
This set of extensions provides a variety of utility functions for common conversions in Swift. It includes functions for converting between numeric types, temperature units, distance units, and power units, as well as formatting a value as a price with a specified currency symbol. The roundTo function allows for rounding a value to a specific number of decimal places and is a mutating function, modifying the value in place.
-
Converts the value to an integer.
Example:
let doubleValue = 42.75 let integerValue = doubleValue.toInt() print(integerValue) // Output: 42
Declaration
Swift
func toInt() -> Int
Return Value
An integer representation of the value.
-
Converts the value to its string representation.
Example:
let number = 123 let stringValue = number.toString() print(stringValue) // Output: "123"
Declaration
Swift
func toString() -> String
Return Value
A string representation of the value.
-
Converts the value to a formatted price string with the specified currency symbol.
Example:
let priceValue = 25.99 let formattedPrice = priceValue.toPrice(currency: "$") print(formattedPrice) // Output: "$25.99"
Declaration
Swift
func toPrice(currency: String) -> String
Parameters
currency
The currency symbol to be used in the formatted string.
Return Value
A string representation of the value as a formatted price.
-
Converts the temperature from Celsius to Fahrenheit.
Example:
let celsiusTemperature = 25.0 let fahrenheitTemperature = celsiusTemperature.celsiusToFahrenheit() print(fahrenheitTemperature) // Output: 77.0
Declaration
Swift
func celsiusToFahrenheit() -> Double
Return Value
A double value representing the temperature in Fahrenheit.
-
Converts the temperature from Fahrenheit to Celsius.
Example:
let fahrenheitTemperature = 77.0 let celsiusTemperature = fahrenheitTemperature.fahrenheitToCelsius() print(celsiusTemperature) // Output: 25.0
Declaration
Swift
func fahrenheitToCelsius() -> Double
Return Value
A double value representing the temperature in Celsius.
-
Converts the temperature from Celsius to Kelvin.
Example:
let celsiusTemperature = 77.0 let klevinTemperature = celsiusTemperature.celsiusToKelvin print(klevinTemperature) // Output: 350.15
Declaration
Swift
func celsiusToKelvin() -> Double
Return Value
A double value representing the temperature in Kelvin.
-
Converts the temperature from Kelvin to Celsius.
Example:
let kelvinTemperature = 77.0 let celsiusTemperature = klevinTemperature.klevinToCelsius() print(celsiusTemperature) // Output: -196.15
Declaration
Swift
func kelvinToCelsius() -> Double
Return Value
A double value representing the temperature in Celsius.
-
Converts the temperature from Fahrenheit to Kelvin.
Example:
let fahrenheitTemperature = 10 let kelvinTemperature = fahrenheitTemperature.fahrenheitToKelvin() print(kelvinTemperature) // Output: 260.93
Declaration
Swift
func fahrenheitToKelvin() -> Double
Return Value
A double value representing the temperature in Kelvin.
-
Converts the temperature from Kelvin to Fahrenheit.
Example:
let kelvinTemperature = 300.0 let fahrenheitTemperature = kelvinTemperature.kelvinToFahrenheit() print(fahrenheitTemperature) // Output: 80.33
Declaration
Swift
func KelvinToFahrenheit() -> Double
Return Value
A double value representing the temperature in Fahrenheit.
-
Converts the distance from kilometers to miles.
Example:
let kilometers = 10.0 let miles = kilometers.kilometerToMiles() print(miles) // Output: 6.21371
Declaration
Swift
func kilometerToMiles() -> Double
Return Value
A double value representing the distance in miles.
-
Converts the distance from miles to kilometers.
Example:
let miles = 6.21371 let kilometers = miles.milesToKilometer() print(kilometers) // Output: 10.0
Declaration
Swift
func milesToKilometer() -> Double
Return Value
A double value representing the distance in kilometers.
-
Converts power from kilowatts to horsepower.
Example:
let kilowatts = 50.0 let horsepower = kilowatts.kwToPs() print(horsepower) // Output: 67.99
Declaration
Swift
func kwToPs() -> Double
Return Value
A double value representing the power in horsepower.
-
Converts power from horsepower to kilowatts.
Example:
let horsepower = 67.99 let kilowatts = horsepower.psToKw() print(kilowatts) // Output: 50.0
Declaration
Swift
func psToKw() -> Double
Return Value
A double value representing the power in kilowatts.
-
Convert
Double
toDecimal
, rounding it toscale
decimal places.Declaration
Swift
func roundedDecimal(to scale: Int = 0, mode: NSDecimalNumber.RoundingMode = .plain) -> Decimal
Parameters
scale
How many decimal places to round to. Defaults to
0
.mode
The preferred rounding mode. Defaults to
.plain
.Return Value
The rounded
Decimal
value.