{"id":34893,"date":"2022-02-02T16:16:49","date_gmt":"2022-02-02T21:16:49","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=34893"},"modified":"2024-05-20T11:26:55","modified_gmt":"2024-05-20T15:26:55","slug":"decimal-vs-double","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/02\/02\/decimal-vs-double\/","title":{"rendered":"Decimal vs. Double"},"content":{"rendered":"<p><a href=\"https:\/\/www.jessesquires.com\/blog\/2022\/02\/01\/decimal-vs-double\/\">Jesse Squires<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.jessesquires.com\/blog\/2022\/02\/01\/decimal-vs-double\/\"><p>More importantly, <code>Decimal<\/code> <strong>does not<\/strong> conform to either <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/binaryfloatingpoint\"><code>BinaryFloatingPoint<\/code><\/a> or <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/floatingpoint\"><code>FloatingPoint<\/code><\/a>, but begins its protocol conformance at <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/signednumeric\"><code>SignedNumeric<\/code><\/a>, which has the <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/signednumeric\/2883859-negate\">single requirement<\/a> to implement <code>func negate()<\/code>. This reveals why <code>Decimal<\/code> cannot be a drop-in replacement for any of Swift&rsquo;s floating-point types &mdash; most functionality is defined in the lower floating-point protocols. Similar to how mixing numeric types like <code>Int<\/code> and <code>Double<\/code> in a single expression is a compiler error in Swift, <code>Decimal<\/code> does not play nicely with the other numerics. Additionally, some common functionality is missing. For example, <code>Decimal<\/code> does not conform to <code>Strideable<\/code> like the other numerics, which means you cannot create ranges of <code>Decimal<\/code> values. Depending on your problem domain, <code>Decimal<\/code> can be difficult to adopt.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/cocoaphony\/status\/1487599196928626689\">Rob Napier<\/a>:<\/p>\n<blockquote cite=\"https:\/\/cocoaphony\/status\/1487599196928626689\"><p>I don&rsquo;t generally recommend <code>Decimal<\/code> for money. I recommend <code>Int<\/code>, and store everything in the base unit, most commonly &ldquo;cents.&rdquo; It&rsquo;s more efficient and often easier to use.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/robertmryan\/status\/1487624401356943360\">Rob Ryan<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/robertmryan\/status\/1487624401356943360\">\n<p>Two benefits of <code>Decimal<\/code>: (1) You can do precise decimal calculations &#8230; e.g. add <code>Double<\/code> of 0.1 ten times &#x2260; 1.0 (!); (2) You want to enjoy more significant digits &#8230; e.g. print <code>Double<\/code> representation of 12345678901234567890 and it&rsquo;s not actually 12345678901234567890.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2011\/06\/05\/ddmathparser\/\">DDMathParser<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2006\/10\/06\/international-money-in-cocoa\/\">International Money in Cocoa<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Jesse Squires: More importantly, Decimal does not conform to either BinaryFloatingPoint or FloatingPoint, but begins its protocol conformance at SignedNumeric, which has the single requirement to implement func negate(). This reveals why Decimal cannot be a drop-in replacement for any of Swift&rsquo;s floating-point types &mdash; most functionality is defined in the lower floating-point protocols. Similar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-02-02T21:16:52Z","apple_news_api_id":"fbbc36ce-09ba-4425-bf9a-5b7688f4c1fa","apple_news_api_modified_at":"2024-05-20T15:27:00Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/A-7w2zgm6RCW_mlt2iPTB-g","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,669,31,2078,30,2077,259,71,901],"class_list":["post-34893","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-floating-point","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-math","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=34893"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34893\/revisions"}],"predecessor-version":[{"id":43324,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34893\/revisions\/43324"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=34893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=34893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=34893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}