PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How to compare decimal values

 August 10, 2022     compare, decimal, ios, objective-c     No comments   

Issue


I have a problem with comparison two decimal values.
I have a text field that contains number like 0.123456 and NSNumber that contains 0.000001.
Maximum fraction digits of both is 6. Minimum - 0
I've tried to do it like that:

NSNumberFormatter *decimalFormatter = [[NSNumberFormatter alloc] init];
[decimalFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
[decimalFormatter setMaximumFractionDigits:6];

double sum = [[decimalFormatter numberFromString:self.summTextField.text] doubleValue];

if (self.minSum != nil) {
    if (sum < [self.minSum doubleValue]) {
        return NO;
    }
}

But i have a problem, that sometimes 0.123456 = 0,123455999... or 0,123456000000...01 For example @0.000001 doubleValue < @0.000001 doubleValue - TRUE.


How can I compare to NSNumber with a fractional part, to be sure that it will be correct?


Thanks in advance.


Solution

You can round your value, if you worried about fractional part... Something like this:

-(double)RoundNormal:(double) value :(int) digit
{
    value = round(value * pow(10, digit));
    return value / pow(10, digit);
}

And then compare it.



Answered By - Konstantin
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing