Sams Teach Yourself Objective-C in 24 Hours by Jesse Feiler
Author:Jesse Feiler
Language: eng
Format: epub
ISBN: 9780132939874
Publisher: Sams Publishing
* * *
By the Way: Floats, Decimals, and Currencies
Calculations on currencies other than simple addition and subtraction need to take into account the issues involved with rounding fractions. This example does not get into those considerations.
* * *
That is a good start, and it is one that many people would use. This means that the code inside the method practically writes itself:
return units * exchangeRate;
But remember that in Objective-C it is often preferable to use the Objective-C classes rather than raw C types. A better method header might be
– (NSNumber *) convertCurrency: (NSNumber *)units
withRate: (NSNumber *)exchangeRate;
The difference between the first version in which the arguments and return value are all float types and the second in which they are NSNumber objects is that that the latter is more generalized. NSNumber objects can be created from and returned as a multitude of different types. Here are the getters for NSNumber:
• boolValue
• charValue
• decimalValue
• doubleValue
• floatValue
• intValue
• integerValue
• longLongValue
• longValue
• shortValue
• unsignedCharValue
• unsignedIntegerValue
• unsignedIntValue
• unsignedLongLongValue
• unsignedLongValue
• unsignedShortValue
This allows any necessary conversions to happen internally to the NSNumber arguments. You can use and reuse the method. If some types of values are going to be problematic, you can manage them inside the conversion method where they are used.
While you are thinking of generalizing the method, consider the fact that the name is more specific than needed. As implemented at this point, the method works for currency conversion as well as conversions of units of measure. Why limit its usefulness only to currency conversion if it is not necessary? You can always come back later and change the generalized method to a more specific one. Going in the other direction can be a bit more work.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Android | E-Readers |
Handheld & Mobile Devices | iPad |
iPhone | Programming & App Development |
Tablets |
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6532)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3340)
Odoo 15 Development Essentials by Daniel Reis(2814)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2529)
Learning Angular - Second Edition by Christoffer Noring(2358)
Pride and Prejudice by Jane Austen(2350)
Mobile Forensics Cookbook by Igor Mikhaylov(2017)
Computers For Seniors For Dummies by Nancy C. Muir(1995)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1872)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1797)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1789)
1936941139 (N) by Bob Rosenthal(1731)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1725)
Hands-On Internet of Things with MQTT by Tim Pulver(1706)
Android App Development by Franceschi Hervé J.;(1702)
Ember.js in Action by Joachim Haagen Skeie(1689)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1656)
