Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make up the number of digits? #2

Open
birdmichael opened this issue Apr 26, 2021 · 3 comments
Open

How to make up the number of digits? #2

birdmichael opened this issue Apr 26, 2021 · 3 comments

Comments

@birdmichael
Copy link

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

@birdmichael
Copy link
Author

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
Copy link
Author

Thank you for your contribution.

@birdmichael
Copy link
Author

change Double to BinaryInteger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant