SassNumber
A Sass numeric value.
Sass numbers are Double
Double
s with units.
Sass has its own rules for numeric equality and integer conversion that use only the first
10 decimal places. Use the SassNumber
methods to convert to integers and test for ranges.
The units on a SassNumber
can describe the result of multiplying and dividing numbers with
primitive units. Best described in the
Sass docs.
The CSS values spec defines several common
units and how to convert between them, for example px to pt. asConvertedTo(...)
implements these conversions which lets you, for example, write a function that accepts any length unit
that you can easily convert to your preferred unit.
Because of units, SassNumber
is not Comparable
Comparable
— there is no ordering relation
possible between “5 cm” and “12 kHz”. It is Equatable
Equatable
though respecting unit conversion
so that “1 cm” == “10 mm”.
Topics
Initializers
init(Double, unit: String?)
init(Double, unit: String?)
init(Double, numeratorUnits: [String], denominatorUnits: [String])
init(Double, numeratorUnits: [String], denominatorUnits: [String])
Initialize a new number from a value and a list of numerator and denominator units.
For example an acceleration:
let g = SassNumber(981, numeratorUnits: ["cm"], denominatorUnits: ["s", "s"])
Declaration
Parameters
double |
The value of the number. |
numeratorUnits |
The names of units applied to the number. |
denominatorUnits |
The names of units whose reciprocals are applied to the number. |
Throws
SassFunctionError.uncancelledUnits(...)
if units for the same dimension are listed in
both numeratorUnits
and denominatorUnits
.
Properties
var double: Double
var double: Double
The underlying sign and magnitude of the number.
Fairly meaningless without understanding the number’s units.
Take care using this value directly because Sass and Swift use different tolerances for comparison and integer conversion.
- Use
asInt()
to check aSassNumber
is an integer instead ofInt.init(exactly:)
Int.init(exactly:)
. (It could be thatInt(exactly: n.double)
would fail.) - Use
asIn(range:)
to check a floating pointSassNumber
is within a range and convert it to a SwiftDouble
Double
within the range. (It could be thatrange.contains(n.double)
would fail.) - If you must compare floating-point
SassNumber
s then consistently compare theSassNumber
s themselves, not thedouble
s within. Similarly, use theSassNumber
itself as a dictionary key.
Declaration
public var double: Double { get }
Comparison
func asInt() -> Int
func asInt() -> Int
The integer value of this number.
This has the same role as Int.init(exactly:)
Int.init(exactly:)
but maps more floating point values to
the same integer according to Sass rules.
Declaration
public func asInt() throws -> Int
Throws
SassFunctionError.notInteger(...)
if the number is not an integer
according to Sass’s rounding rules.
Return Value
The integer that this SassNumber
exactly represents.
func asIn(range: ClosedRange<Double>) -> Double
func asIn(range: ClosedRange<Double>) -> Double
The value of this number within a closed range.
If you have an integer range than do asInt()
first and work on that.
Declaration
public func asIn(range: ClosedRange<Double>) throws -> Double
Throws
SassFunctionError.notInRange(...)
if the number is not in
the range, using Sass’s rounding rules at the ends of the range.
Return Value
The Double
Double
value corresponding to this SassValue
in range
.
func asIn(range: Range<Double>) -> Double
func asIn(range: Range<Double>) -> Double
The value of this number within a half-open range.
If you have an integer range than do asInt()
first and work on that.
Declaration
Throws
SassFunctionError.notInRange(...)
if the number is not in
the range, using Sass’s rounding rules at the ends of the range.
Return Value
The Double
Double
value corresponding to this SassValue
in range
.
Units
var hasNoUnits: Bool
var hasNoUnits: Bool
func checkNoUnits()
func checkNoUnits()
Throw an error if the number has any units.
Declaration
public func checkNoUnits() throws
func hasUnit(name: String) -> Bool
func hasUnit(name: String) -> Bool
func checkHasUnit(name: String)
func checkHasUnit(name: String)
Throw an error unless the number has exactly the single unit.
Declaration
public func checkHasUnit(name: String) throws
var numeratorUnits: [String]
var numeratorUnits: [String]
var denominatorUnits: [String]
var denominatorUnits: [String]
The names of the ‘denominator’ units.
Declaration
public var denominatorUnits: [String] { get }
func asConvertedTo(numeratorUnits: [String], denominatorUnits: [String]) -> SassNumber
func asConvertedTo(numeratorUnits: [String], denominatorUnits: [String]) -> SassNumber
The equivalent SassNumber
converted to the requested units.
Only units described in the CSS spec as ‘convertible’ can be converted.
A number without any units can be ‘converted’ to any set of units.
Declaration
public func asConvertedTo(
numeratorUnits: [String] = [], denominatorUnits: [String] = []
) throws -> SassNumber
Throws
SassFunctionError
If the requested units are invalid, or if the number’s units
are not convertible to the requested units.
Misc
static func ==(lhs: SassNumber, rhs: SassNumber) -> Bool
static func ==(lhs: SassNumber, rhs: SassNumber) -> Bool
Two SassNumber
s are equal iff:
- Neither have units and their values are the same to 10 decimal places; or
- They have convertible units and, when both converted to the same units, have values that are equal to 10dp.
Declaration
public static func == (lhs: SassNumber, rhs: SassNumber) -> Bool
func hash(into: inout Hasher)
func hash(into: inout Hasher)
func accept<V, R>(visitor: V) -> R
func accept<V, R>(visitor: V) -> R
Take part in the SassValueVisitor
protocol.
Declaration
public override func accept<V, R>(visitor: V) throws -> R
where V: SassValueVisitor, R == V.ReturnType
var description: String
var description: String