In iOS, how can use NumberFormatter to format the decimal part of a number into its fractional representations.
For example I would like to format the number 2.375
as 2 3/8
.
Can this be done with NumberFormatter?
In iOS, how can use NumberFormatter to format the decimal part of a number into its fractional representations.
For example I would like to format the number 2.375
as 2 3/8
.
Can this be done with NumberFormatter?
No. You would need to implement your own Rational Formatter. You can use this answer from Martin R as a starting point. You can do something like:
class RationalFormatter: Formatter {
let precision: Double = 1.0E-6
override public func string(for obj: Any?) -> String? {
guard let value = obj as? Double else { return nil }
let whole = modf(value).0
var x = modf(value).1
var a = x.rounded(.down)
var (h1, k1, numerator, denominator) = (1, 0, Int(a), 1)
while x - a > precision * Double(denominator) * Double(denominator) {
x = 1.0/(x - a)
a = x.rounded(.down)
(h1, k1, numerator, denominator) = (numerator, denominator, h1 + Int(a) * numerator, k1 + Int(a) * denominator)
}
var string = ""
if whole < 0 || numerator < 0 {
string += "-"
}
if whole != 0 {
string += String(Int(abs(whole)))
}
if whole != 0 && numerator != 0 {
string += " "
}
if numerator != 0 {
string += "\(abs(numerator))/\(abs(denominator))"
}
return string
}
}
Usage:
let double = 2.375
let rationalFormatter = RationalFormatter()
let formatted = rationalFormatter.string(for: double) // "2 3/8"