IOS-compute imprecise problems such as floatvalue,doublevalue data with high computational accuracy requirements

Source: Internet
Author: User

1. Problem Description: Double type 9 returned by server. 9,4. 94 of the number 29.89999961   ==> reserved 2 decimal point 9.  39.9000000000000003 ==> reserved 2 digits decimal point 9.  - 4 , other workaround: The data returned by the server is all set to type string, so the problem does not occur in memory, CPU calculation, float is more appropriate, in general, float

Double The difference between float is a double precision, a valid number 16 bits, and a float precision of 7 bits. But double consumes twice times the memory is float, double operation speed is much slower than float, C language math function name double and float different, do not write wrong, can use single precision do not use double precision (to save memory, speed up the operation speed).

One of the ways to resolve:

when designing to the calculation of money. But the use of double type data is unstable.1: Controls the precision of a double typeDoubleABC =0.1-0.01-0.00001009; Nsnumberformatter*NF =[[Nsnumberformatteralloc]init]; [NF Setmaximumintegerdigits:8]; NSNumber*number = [Nfnumberfromstring:[nsstring stringWithFormat:@"%.8LF", ABC]]; NSLog (@"Number is ---%lf", [Numberdoublevalue]);2: Calculation method for double type without loss of precision-(CGFloat) Addreebackmoneywithamount: (cgfloat) Amount Tomoney: (cgfloat) tomoney{nsstring*AMOUNTSTR = [NSString stringWithFormat:@"%.08LF", amount]; NSString*TOMONEYSTR = [NSString stringWithFormat:@"%.08LF", Tomoney]; Nsdecimalnumber*amountnum =[Nsdecimalnumber Decimalnumberwithstring:amountstr]; Nsdecimalnumber*tomoneynum =[Nsdecimalnumber Decimalnumberwithstring:tomoneystr]; DoubleXiaofee =0.001210000; Nsdecimalnumber*feenum = [Nsdecimalnumber decimalnumberwithstring:[nsstring stringWithFormat:@"%.8LF", Xiaofee]]; Nsdecimalnumber*resultnum =[Amountnum Decimalnumberbysubtracting:tomoneynum]; Nsdecimalnumber*subtracfeenum =[Resultnum Decimalnumberbysubtracting:feenum]; return[Subtracfeenum doublevalue];;}

Give me a chestnut: calculate 0.1*999999 and see what happens?

-(void) testdecimalnumber {     double0.01;      Double 999999 ;      double d3 = d1 * D2;       NSLog (@ "%@", N3);  }

And we think of the results: 9999.99 different

The solution to this problem is as follows:

-(void) testdecimalnumber {     nsdecimalnumber* n1 = [Nsdecimalnumber       Decimalnumberwithstring:[nsstring    stringWithFormat:@ "%f", D1]];    Nsdecimalnumber* n2 = [Nsdecimalnumber decimalnumberwithstring:[nsstring stringwithformat:@ "%f  ", D2]];    Nsdecimalnumber* n3 = [N1 DECIMALNUMBERBYMULTIPLYINGBY:N2]; NSLog (@ "%@", number);}

You can convert a float, double to an object of type Nsdecimalnumber in the calculation. You can perform operations of + 、-、 *,/.

Usage Rounding:

-(nsstring*) notrounding: (float) Price afterpoint: (nsinteger) position{    Nsdecimalnumberhandler * Roundinghandler = [Nsdecimalnumberhandler decimalnumberhandlerwithroundingmode:nsroundplain scale: Position Raiseonexactness:no Raiseonoverflow:no Raiseonunderflow:no Raiseondividebyzero:no];    Nsdecimalnumber* returnnumber;    Nsdecimalnumber* ouncesdecimal = [[Nsdecimalnumber alloc]initwithfloat:price];     = [Ouncesdecimal decimalnumberbyroundingaccordingtobehavior:roundinghandler];     return [NSString stringWithFormat:@ "%@", Returnnumber];}

Test data: 1.235

Results:

IOS-compute imprecise problems such as floatvalue,doublevalue data with high computational accuracy requirements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.