A lot of decimal calculations are needed in a recent iOS project, which requires a high level of precision. As we all know, for the computer float,double such floating-point numbers are stored in scientific notation, so there are computational errors, so they can not be directly related to the calculation.
There are corresponding high-precision calculations in each language, such as the BigDecimal class in Java, and the decimal type in C # is used to solve high-precision computational problems, such as currency calculations.
After looking at the API given by Apple, we found a class-----nsdecimalnumber
Let's take a look at the method definition we need in it:
-decimalnumberbyadding :
-Decimalnumberbysubtracting:
-Decimalnumberbymultiplyingby:
-Decimalnumberbydividingby:
The above 4 methods represent the subtraction of our arithmetic in each other.
This should be a good understanding of how to do our high-precision calculations, the following is the example I wrote for understanding, Addendvalue and augendvalue for the nsstring type, A high-precision calculation can be done by converting the number of numbers that require operations into the nsstring type of the corresponding number of digits:
Nsdecimalnumber *addendnumber = [Nsdecimalnumber decimalnumberwithstring:addendvalue]; Nsdecimalnumber *augendnumber = [Nsdecimalnumber decimalnumberwithstring:augendvalue]; return [Addendnumber Decimalnumberbyadding:augendnumber];
This article is from the "It-fan" blog, make sure to keep this source http://iteafan.blog.51cto.com/784291/1585053
Ios:objective-c High Accuracy Calculation