hockeyman
hockeyman

Reputation: 1183

How to check if float has only 2 decimal places not equal to 0

I have NSSlider, and I rect to it's changes. But I want my action to work only if slider's float value is for example 2.230000 or 3.410000.

if (floatValue is y.xx0000) {
    doSomething;
}

I mean I want to do some action only if my float has only 2 decimal places not equal to 0. How could I do it?

Upvotes: 0

Views: 911

Answers (2)

FlorianK
FlorianK

Reputation: 440

I'm a bit late to this game, but I found this question while searching for something similar. It sounds to me like what you're trying to do is to round the value that the slider is set to to 2 decimal places. That way the thing and/or calculation you're trying to configure with the slider, will always work as though it only works for exact 2 decimal values.

Alternatively, you could check how large the distance is between your float value, and the value rounded to 2 decimal places. And then put a threshold of for example 0.005 for when it's to far away.

Upvotes: 0

Stephen Canon
Stephen Canon

Reputation: 106167

The only float values that have only two non-zero fractional digits are numbers of the form n.00, n.25, n.50, or n.75. All other values have more than two non-zero fractional digits. Your example, 3.41, for example, isn't really "3.41". Instead it's:

3.410000000000000142108547152020037174224853515625

and "2.23" is actually:

2.229999999999999982236431605997495353221893310546875

So what are you really trying to do?

Upvotes: 4

Related Questions