1. Our app has a judgment. When the value entered by the user is less than or equal to the remaining balance, the user will pop up the window and the code will not be executed downwards.
When the user enters 0.01, the result of poking is: 0.10000.... When the user's balance is 0.01, the result of poking the network request is: 0.009999...
When the user enters 66.05, the result of po is: 66.049999... When the user’s balance is 66.05, the result of the return of the request for poaching the network is: 66.049999...
Therefore, when the data is large, there will be no impact, because the data input by the user and the data returned by the network are both turned into inaccurate small values; however, when the data is small, the data input by the user is not converted, and the data returned by the network is converted into inaccurate small values, resulting in the user input always greater than the data returned by the network, and the code cannot be executed downwards;
Solution: Use NSDecimalNumber to convert float and double data into objects of type NSDecimalNumber for +, -, *, and / calculation, and then take the value.
- (double)DecimalNumber:(double)num1 num2:(double)num2 { NSDecimalNumber *n1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",num1]]; NSDecimalNumber *n2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",num2]]; NSDecimalNumber *n3 = [n1 decimalNumberBySubtracting:n2]; return ; } // Call:double result = [self DecimalNumber: 2.01 num2: 2]; // The result is 0.01
in:
decimalNumberBySubtracting: n1 - n2, returns the result value of n1 - n2
decimalNumberByMultiplyingBy:n1 * n2,
decimalNumberByDividingBy:n1 / n2,
decimalNumberByAdding:n1 + n2,
compare: compare n1 and n2, return the comparison result;
2. Round the data to obtain the results
- (NSString*)Rounding:(float)number afterPoint:(NSInteger)position { NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode: NSRoundPlain scale: position raiseOnExactness: NO raiseOnOverflow: NO raiseOnUnderflow:NO raiseOnDivideByZero: NO]; NSDecimalNumber *floatDecimal = [[NSDecimalNumber alloc] initWithFloat: number]; NSDecimalNumber *resultNumber = [floatDecimal decimalNumberByRoundingAccordingToBehavior:handler]; return [NSString stringWithFormat:@"%@",resultNumber]; } // Call:NSString *result = [self Rounding:8.00092 afterPoint:3]; // The result is 8.001
Among them, the parameter number is the data that needs to be rounded, and position is the number of digits retained after the decimal point;
High precision requirements
Use the NSDecimalNumber API provided by the system for calculation and finally convert it into a string output display. NSDecimalNumber conversion method:
#import "NSDecimalNumber+Y_Add.h" @implementation NSDecimalNumber (Y_Add) + (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value{ return [self y_decimalNumberWithFloat:value scale:2]; } + (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value scale:(short)scale{ return [self y_decimalNumberWithFloat:value roundingMode:NSRoundBankers scale:scale]; } + (NSDecimalNumber *)y_decimalNumberWithFloat:(float)value roundingMode:(NSRoundingMode)roundingMode scale:(short)scale{ return [[[NSDecimalNumber alloc] initWithFloat:value] y_decimalNumberHandlerWithRoundingMode:roundingMode scale:scale]; } + (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value{ return [self y_decimalNumberWithDouble:value scale:2]; } + (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value scale:(short)scale{ return [self y_decimalNumberWithDouble:value roundingMode:NSRoundBankers scale:scale]; } + (NSDecimalNumber *)y_decimalNumberWithDouble:(double)value roundingMode:(NSRoundingMode)roundingMode scale:(short)scale{ return [[[NSDecimalNumber alloc] initWithFloat:value] y_decimalNumberHandlerWithRoundingMode:roundingMode scale:scale]; } /** * <#Description#> * * @return <#return value description#> */ - (NSDecimalNumber *)y_decimalNumberHandler{ return [self y_decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:2]; } - (NSDecimalNumber *)y_decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale{ NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingMode scale:scale raiseOnExactness:NO raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES]; return [self decimalNumberByRoundingAccordingToBehavior:handler]; } @end
Normal precision requirements
Use the mathematical operations provided by the system to perform calculations and finally convert them into string output display. Methods to retain N positions after the decimal point:
/** * formatterNumber .00 Two decimal places * * @param number <#number description#> * * @return <#return value description#> */ + (NSString *)y_formatterNumber:(NSNumber *)number{ return [self y_formatterNumber:number fractionDigits:2]; } + (NSString *)y_formatterNumber:(NSNumber *)number fractionDigits:(NSUInteger)fractionDigits{ NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setMaximumFractionDigits:fractionDigits]; [numberFormatter setMinimumFractionDigits:fractionDigits]; return [numberFormatter stringFromNumber:number]; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.