MovingNumbersView
MovingNumbersView copied to clipboard
How to make up the number of digits?
How to make up the number of digits?
just list 01
to 02
update
private func getAllDigitsInAscendingSignificance(number: Int, minimumFractionDigits: Int) -> [Int] {
if number == 0 {
guard minimumFractionDigits <= 1 else {
return Array(repeating: 0, count: minimumFractionDigits)
}
return [0]
}
var rest = abs(number)
var digits: [Int] = []
while rest >= 10 {
let quotient = rest / 10
let d = rest - (quotient * 10)
digits.append(d)
rest = quotient
}
if rest != 0 {
digits.append(rest)
}
if digits.count < minimumFractionDigits {
let zeroArray = Array(repeating: 0, count: (max(minimumFractionDigits - digits.count, 1)))
digits.append(contentsOf: zeroArray)
}
return digits
}
and
/// Get visual elements for the whole number part
/// i.e. 1234 -> 1,234
func getWholeVisualElements(whole: Int) -> [VisualElementType] {
let wholeDigits = getAllDigitsInAscendingSignificance(number: whole, minimumFractionDigits: minimumFractionDigits)
var wholeElements: [VisualElementType] = []
for (i, digit) in wholeDigits.enumerated() {
if i != 0 && i % 3 == 0 && wholeDigits.last != 0 { // eg.: `000010000` do not 000010,000`, `0001` do not `0,001`
wholeElements.append(.comma(position: i+1))
}
wholeElements.append(.digit(digit, position: i+1))
}
return wholeElements
}
Thank you for your contribution.
change Double
to BinaryInteger