0

I would like to count the digit to the right of the decimal point while only being zeros

Input: 1.00000000

Desired Output: 8

Code Tried:

let decimalDigits = String(totalValue).split(separator: ".").last?.count ?? 0

extension Double {
    func decimalCount() -> Int {
        if self == Double(Int(self)) {
            return 0
        }

        let integerString = String(Int(self))
        let doubleString = String(Double(self))
        let decimalCount = doubleString.count - integerString.count - 1

        return decimalCount
    }
}

PSUEDO CODE:

> if to right of decimal all zeros 
> count number of zeros

How would I account for decimal count if all zeros to the right of the decimal?

In the above code I can account for number of decimals. But I cannot account for number of decimals if they are all zeros.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • Doing this on `Double` won't be reliable (see https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Probably want to use Decimal instead https://developer.apple.com/documentation/foundation/decimal – jnpdx Jun 22 '23 at 19:27
  • You can’t count the number of decimal digits in meaningful way but if you could the `if` condition would catch the all zeros numbers so remove that – Joakim Danielson Jun 22 '23 at 20:26
  • I just need a way to count all the decimals if they are only zero. I dont know how to accomplish this. – adalovelacy Jun 22 '23 at 20:33
  • Then remove the `if` condition. – Joakim Danielson Jun 22 '23 at 20:36
  • removing the if condition does not solve the problem @JoakimDanielson – adalovelacy Jun 22 '23 at 20:37
  • 1
    The real problem is that what you are trying to do is pointless. Did you read the linked question in the first comment? – Joakim Danielson Jun 22 '23 at 20:43
  • This is fundamentally not how doubles work. There is an infinite number of 0s in `1.00000000`. The fact that you're shown 8 instead of 7 or 9 is merely an arbitrary choice in presentation (done by `print`, `String.init`, `NumberFormatter`, or some other means of making a string from an integer). – Alexander Jun 22 '23 at 20:49

1 Answers1

1

The input you're describing is a String, not a Double (or any kind of number). Numbers are defined by their value, not their digit-representation. So any solution is going to work on String.

Assuming you've already trimmed the String and know that it's only digits, the following would work:

let value = "1.00000000"

func countFractionalDigits(_ string: String) -> Int {
    guard let decimalIndex = string.firstIndex(of: ".") else { return 0 }
    return string.distance(from: decimalIndex, to: string.endIndex) - 1
}

countFractionalDigits(value)   // 8

If you need this to work with Double, or other kinds of numbers, then you'll have to apply a Formatter to turn it into a String, at which point you will need to decide how many digits there are. Numbers do not carry any information about the digits that were used to create them. (How could they? How many trailing zeros are in the result of "8.00 / 2.0"?)

Rob Napier
  • 286,113
  • 34
  • 456
  • 610