Centurion
Centurion

Reputation: 14304

How to get NSNumber value length?

I need to check total digit count from NSNumber. Most easy way is to convert it to NSString and then check its length. The other way would be to take integer value, perform loop by dividing with 10. Just wondering are there other options :)

Upvotes: 2

Views: 2253

Answers (1)

GoZoner
GoZoner

Reputation: 70135

The log10() function will get you close. Something like:

ceil (log10 (x))

for example:

> ceil (log10 (200))
3
> ceil (log10 (9999))
4

Maybe add in an fabs as:

ceil (log10 (fabs (x)))

For fractional numbers, the above wouldn't work. But, you could try splitting into integer+fraction and working on the two independently.

Upvotes: 5

Related Questions