SassNumber

Swift
public final class SassNumber: SassValue

A Sass numeric value.

Sass numbers are DoubleDoubles 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 ComparableComparable — there is no ordering relation possible between “5 cm” and “12 kHz”. It is EquatableEquatable though respecting unit conversion so that “1 cm” == “10 mm”.


Topics

Initializers

init(Double, unit: String?)

Initialize a new number from a value and optionally a unit.

Declaration
Swift
public init(_ double: Double, unit: String? = nil)

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
Swift
public init(
    _ double: Double, numeratorUnits: [String] = [],
    denominatorUnits: [String] = []) throws
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

The underlying sign and magnitude of the number.

Fairly meaningless without understanding the number’s units.

warning

Take care using this value directly because Sass and Swift use different tolerances for comparison and integer conversion.

Declaration
Swift
public var double: Double { get }

Comparison

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
Swift
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

The value of this number within a closed range.

If you have an integer range than do asInt() first and work on that.

Declaration
Swift
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 DoubleDouble value corresponding to this SassValue in range.

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
Swift
public func asIn(range: Range<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 DoubleDouble value corresponding to this SassValue in range.

Units

var hasNoUnits: Bool

Is the number free of units?

Declaration
Swift
public var hasNoUnits: Bool { get }

func checkNoUnits()

Throw an error if the number has any units.

Declaration
Swift
public func checkNoUnits() throws

func hasUnit(name: String) -> Bool

Does the number have exactly this unit?

Declaration
Swift
public func hasUnit(name: String) -> Bool

func checkHasUnit(name: String)

Throw an error unless the number has exactly the single unit.

Declaration
Swift
public func checkHasUnit(name: String) throws

var numeratorUnits: [String]

The names of the ‘numerator’ units.

Declaration
Swift
public var numeratorUnits: [String] { get }

var denominatorUnits: [String]

The names of the ‘denominator’ units.

Declaration
Swift
public var denominatorUnits: [String] { get }

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
Swift
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

Two SassNumbers are equal iff:

  1. Neither have units and their values are the same to 10 decimal places; or
  2. They have convertible units and, when both converted to the same units, have values that are equal to 10dp.
Declaration
Swift
public static func == (lhs: SassNumber, rhs: SassNumber) -> Bool

func hash(into: inout Hasher)

Hash the value.

Declaration
Swift
public override func hash(into hasher: inout Hasher)

func accept<V, R>(visitor: V) -> R

Take part in the SassValueVisitor protocol.

Declaration
Swift
public override func accept<V, R>(visitor: V) throws -> R
where V: SassValueVisitor, R == V.ReturnType

var description: String

A short description of the value.

Declaration
Swift
public override var description: String { get }