Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to accurately calculate the decimals in iOSNSDecimalNumber

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to accurately calculate the decimals in iOSNSDecimalNumber. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

NSDecimalNumber this API, this class provides fixed-point algorithm functionality for OC programs, it is designed without loss of precision and can be pre-set to refine the rule of decimal calculation, it is better than floating-point numbers to express currency, as a price, its calculation is relatively complex and relatively time-consuming.

Take a chestnut:

Float a = 0.01tint b = 99999999ntdouble c = 0.0x c = a * bscape NSLog (@ "% f", c); NSLog (@ "% 2f", c)

2017-08-02 15 41R 25.620 DecimalNumber [3014 purl 155231] 1000000.000000

2017-08-02 15 41R 25.620 DecimalNumber [3014 purl 155231] 1000000.00

It has obviously been distorted.

So wouldn't it be better for us to change the type and increase the precision?

C = a * (double) b / NSLog (@ "% f", c); NSLog (@ "% .2f", c)

However it doesn’t make any difference,

2017-08-02 16 purl 1632.293 DecimalNumber [3144 purl 167125] 999999.967648

2017-08-02 16 rides 1632.294 DecimalNumber [3144 rides 167125] 999999.97

What the heck is all this? I learned math in vain.

Finally, I thought of a method. I converted it to a string, and then to the double type. I didn't expect the precision to meet the requirements.

NSString * aString = [NSString stringWithFormat:@ "% .2f", a]; NSString * bSting = [NSString stringWithFormat:@ "% .2f", (double) b]; c = [aString doubleValue] * [bString doubleValue]; NSLog (@ "% .2f", c)

2017-08-02 16 purl 2732.590 DecimalNumber [3252 purl 172900] 999999.99

But it looks a little awkward, but I don't want to talk about it. I feel unprofessional when I look at it. The following calculation method is provided through NSDecimalNumber, which is the officially recommended currency calculation API with high accuracy. There are separate APIs for multiplication and division calculation.

NSDecimalNumber * decimalNumber1 = [NSDecimalNumber decimalNumberWithString:aString]; NSDecimalNumber * decimalNumber2 = [NSDecimalNumber decimalNumberWithString:bString]; NSDecimalNumber * result = [decimalNumber1 decimalNumberByMultiplyingBy:decimalNumber2]; NSLog (@ "% @", result)

2017-08-02 16 rides 35 purl 32.779 DecimalNumber [3369 rides 177485] 999999.99

In this way, it looks much more comfortable and the accuracy is satisfied.

In the calculation of currency, we should always pay attention to the problem of accuracy. This API is preferred.

After reading the above, do you have any further understanding of how to accurately calculate the decimals in iOSNSDecimalNumber? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report