# Objective-C – Get number of decimals of a double variable

Is there a nice way to get the number of decimals of a double variable in Objective-C?

I am struggling for a while to find a way but with no success.

For example 231.44232000 should return 5.

Thanks in advance.

- iOS Objective-C How to get 2 decimal rounded float value?
- ios - dynamically create and display unicode strings
- Easiest way to truncate float to 2 decimal places?
- Convert Binary to Decimal in Objective C
- Limit a double to two decimal places without trailing zeros
- NSNumberFormatter returns nil on device, not on simulator

### 2 Solutions Collect From Internet About “Objective-C – Get number of decimals of a double variable”

You could, in a loop, multiply by 10 until the fractional part (returned by modf()) is really close to zero. The number of iterations’ll be the answer you’re after. Something like:

```
int countDigits(double num) {
int rv = 0;
const double insignificantDigit = 8;
double intpart, fracpart;
fracpart = modf(num, &intpart);
while ((fabs(fracpart) > 0.000000001f) && (rv < insignificantDigit)) {
num *= 10;
rv++;
fracpart = modf(num, &intpart);
}
return rv;
}
```

Is there a nice way to get the number of decimals of a double variable in Objective-C?

No. For starters, a double stores a number in binary, so there may not even be an exact binary representation that corresponds to your decimal number. There’s also no consideration for the number of significant decimal digits — if that’s important, you’ll need to track it separately.

You might want to look into using NSDecimalNumber if you need to store an exact representation of a decimal number. You could create your own subclass and add the ability to store and track significant digits.

- iOS – How to search a “searchable” ABSource, with ABSourceType | kABSourceTypeSearchableMask
- iOS CoreLocation Altitude
- Is it possible to build iphoneos6.1 projects in Xcode 5, preserving the behaviour of views laid out in an Xcode 4.6.3 storyboard?
- iOS8 Dynamic Frameworks -> CodeSign error: code signing is required for product type 'Framework' in SDK 'iOS 8.3'
- Getting absolute position of CCSprite in cocos2d
- Compare a string by giving two possibilities for each character in Swift
- Crash on Xcode 8.0 GM iPhone Simulator (iOS 10)
- How to implement Haskell's splitEvery in Swift?
- Get current track playing on control center iOS
- Object name from String in Objective-C
- Is there any way we can restrict duplicate entries in core data?
- ios – How to declare static variable?
- Programmatically set image and text on UIButton
- NSBundle pathForResource is NULL
- Xcode 4 & three20 & create IPA archive: No such file or directory