2/16/2024 0 Comments Swift downcast double to intI started out using Double for the values that the user enters and for all calculations. I’m currently working on a project that primarily deals with money calculations and money estimations. I hope this post can serve as a good reference on when and why you might want to use Decimal instead of Double. But, social media is ephemeral and impossible to search - not to mention, I periodically delete my tweets - so that’s where this post comes in. The resulting thread was extremely helpful. I could not find many resources on the web explaining the differences and benefits of Double versus Decimal, so I asked the Apple dev community on Twitter instead. I was recently facing this question, and this post is a summary of everything I’ve learned. You likely know that Double is more precise than Float, but what about Decimal? When should you reach for Decimal instead? But if we are calculating sales tax, then we’ll need to use a floating-point type. For example, if we are counting ticket sales for an event, then Int (or possibly UInt) would be the most appropriate type. ![]() ![]() When we build the model layer of an app, it’s important to choose the right type for the task we want to accomplish. But if you’ve been developing apps for Apple platforms for any amount of time, you’ll recognize another numeric type - Decimal (aka NSDecimalNumber). ![]() Like most other programming languages, Swift provides signed integers of various sizes, corresponding unsigned integers, and a few floating-point types.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |