MovingNumbersView icon indicating copy to clipboard operation
MovingNumbersView copied to clipboard

How to make up the number of digits?

Open birdmichael opened this issue 3 years ago • 3 comments

How to make up the number of digits? just list 01 to 02

birdmichael avatar Apr 26 '21 06:04 birdmichael

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
    }

birdmichael avatar Apr 26 '21 07:04 birdmichael

Thank you for your contribution.

birdmichael avatar Apr 26 '21 07:04 birdmichael

change Double to BinaryInteger

birdmichael avatar Apr 26 '21 08:04 birdmichael