{"id":10,"date":"2007-12-14T10:15:38","date_gmt":"2007-12-14T16:15:38","guid":{"rendered":"http:\/\/todesco-technologies.com\/unboxed\/?p=3"},"modified":"2007-12-14T10:15:38","modified_gmt":"2007-12-14T16:15:38","slug":"why-are-decimals-a-good-idea","status":"publish","type":"post","link":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/2007\/12\/14\/why-are-decimals-a-good-idea\/","title":{"rendered":"Why are decimals a good idea?"},"content":{"rendered":"<p>It&#8217;s easy:\u00a0always use decimals for variables that represent money.<\/p>\n<p>Reason to avoid floats or doubles: They will produce incorrect results when you try to calculate something. I have to add, it will produce errors in <strong>very rare<\/strong> cases. However you can avoid it by using decimals!<\/p>\n<p>You don&#8217;t believe me? Try this: 77.1 * 850 should be exactly 65,535.<\/p>\n<p>decimal correct = (decimal)77.1 * (decimal)850d;<\/p>\n<p>correct will be = 65535<\/p>\n<p>double incorrect = (double)77.1 * (double)850;<\/p>\n<p>incorrect will be = 65534.999999999993<\/p>\n<p>Why is that? That&#8217;s because 0.1 has no exact representation in binary&#8230; it&#8217;s a repeating binary number. It&#8217;s sort of like how 1\/3 has no representation in decimal. 1\/3 is 0.33333333 and you have to keep writing 3&#8217;s forever. If you lose patience, you get something inexact.<\/p>\n<p>So what are the downsides to decimal?<\/p>\n<ol>\n<li>They use a lot more space, 128 bit to be exact. But who cares these days&#8230;<\/li>\n<li>They are slow as a dog. Again, doesn&#8217;t matter if you are not about to program your own spreadsheet application.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy:\u00a0always use decimals for variables that represent money. Reason to avoid floats or doubles: They will produce incorrect results when you try to calculate something. I have to add, it will produce errors in very rare cases. However you can avoid it by using decimals! You don&#8217;t believe me? Try this: 77.1 * 850 &hellip; <a href=\"https:\/\/todescotechnologies.com\/unboxed\/index.php\/2007\/12\/14\/why-are-decimals-a-good-idea\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Why are decimals a good idea?<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}