Skip to content

Commit 688a00a

Browse files
committed
Use a counter instead of array in lengthOfInt and fix algorithm for '0' input
1 parent 3a3da52 commit 688a00a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Foundation/ByteCountFormatter.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,17 @@ open class ByteCountFormatter : Formatter {
353353

354354
// A helper method to return the length of an int
355355
private func lengthOfInt(number: Int) -> Int {
356+
guard number != 0 else {
357+
return 1
358+
}
356359
var num = abs(number)
357-
var length: [Int] = []
360+
var length = 0
358361

359362
while num > 0 {
360-
let remainder = num % 10
361-
length.append(remainder)
363+
length += 1
362364
num /= 10
363365
}
364-
return length.count
366+
return length
365367
}
366368

367369
// Returns the correct string based on the includesValue and includesUnit properties

0 commit comments

Comments
 (0)