{"id":5751,"date":"2015-11-15T11:47:51","date_gmt":"2015-11-15T10:47:51","guid":{"rendered":"http:\/\/www.b.shuttle.de\/hayek\/hayek\/jochen\/wp\/blog-en\/?p=5751"},"modified":"2015-11-15T11:47:51","modified_gmt":"2015-11-15T10:47:51","slug":"rounding-numerical-values-how-to-achieve-round-half-away-from-zero-with-printf","status":"publish","type":"post","link":"https:\/\/wp.jochen.hayek.name\/blog-en\/2015\/11\/15\/rounding-numerical-values-how-to-achieve-round-half-away-from-zero-with-printf\/","title":{"rendered":"rounding numerical values: how to achieve &#8220;round half away from zero&#8221; with printf?"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Rounding#Floating-point_rounding\">https:\/\/en.wikipedia.org\/wiki\/Rounding#Floating-point_rounding<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Rounding#Round_half_away_from_zero\">https:\/\/en.wikipedia.org\/wiki\/Rounding#Round_half_away_from_zero<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/printf_format_string\">https:\/\/en.wikipedia.org\/wiki\/printf_format_string<\/a><\/li>\n<li><a href=\"http:\/\/www.gnu.org\/software\/libc\/manual\/html_node\/Rounding.html\">http:\/\/www.gnu.org\/software\/libc\/manual\/html_node\/Rounding.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/178539\/how-do-you-round-a-floating-point-number-in-perl\">http:\/\/stackoverflow.com\/questions\/178539\/how-do-you-round-a-floating-point-number-in-perl<\/a><\/li>\n<\/ul>\n<p>To be more precise: I actually mean &#8220;<em>considering a given precision<\/em>&#8221; of real numbers.<\/p>\n<p>In Germany (in a financial context at least)\u00a0&#8220;<em>round half away from zero<\/em>&#8221; is being used. \u2026<\/p>\n<p>Look at this \u2013 can you explain it?:<\/p>\n<p style=\"padding-left: 30px\"><code>$ perl -e '$h = 3272.505; printf \"%.2f --- %.10fn\",$h,$h;'<\/code><br \/>\n<code>3272.51 --- 3272.5050000000<br \/>\n<span style=\"line-height: 1.6471\">$ perl -e '$h = 622.775; printf \"%.2f --- %.10fn\",$h,$h;'<br \/>\n<\/span><\/code><code>622.77 --- 622.7750000000<\/code><\/p>\n<p>I think we don&#8217;t see, what the real numbers really are.\u00a03272.505 is supposedly kept as something like\u00a03272.50499999 (&lt;) as supposed to\u00a0622.775, which is supposedly kept closer (&gt;=) to what we see. Apparently the results are acceptable outside the financial world, but within the financial world they are totally unacceptable. I have no idea, why the Perl community has been allowing this for such a long time.<\/p>\n<p>Update 2015-11-21:<\/p>\n<p>I fiddled on a subroutine, that essentially takes parameters like this:<\/p>\n<p style=\"padding-left: 30px\"><code>sprintf(\"%7.2f\",3272.505);<\/code><\/p>\n<p>For the time being I call it like this:<\/p>\n<p style=\"padding-left: 30px\"><code>&amp;round_real_number__half_away_from_zero(<\/code><br \/>\n<code>\u00a0 'v' =&gt;\u00a03272.505,<\/code><br \/>\n<code>\u00a0 'total_length' =&gt; 7,<\/code><br \/>\n<code>\u00a0 'post_decimal_points_digits' =&gt; 2);<\/code><\/p>\n<p>In a way I am quite disappointed, that sprintf does not do, what I expect it to do.\t\t\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/en.wikipedia.org\/wiki\/Rounding#Floating-point_rounding https:\/\/en.wikipedia.org\/wiki\/Rounding#Round_half_away_from_zero https:\/\/en.wikipedia.org\/wiki\/printf_format_string http:\/\/www.gnu.org\/software\/libc\/manual\/html_node\/Rounding.html http:\/\/stackoverflow.com\/questions\/178539\/how-do-you-round-a-floating-point-number-in-perl To be more precise: I actually mean &#8220;considering a given precision&#8221; of real numbers. In Germany (in a financial context at least)\u00a0&#8220;round half away from zero&#8221; is being used. \u2026 Look at this \u2013 can you explain it?: $ perl -e &#8216;$h = 3272.505; printf &#8220;%.2f &#8212; %.10fn&#8221;,$h,$h;&#8217; 3272.51 &#8212; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[666],"tags":[],"class_list":["post-5751","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paO0kP-1uL","jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/5751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/comments?post=5751"}],"version-history":[{"count":0,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/5751\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/media?parent=5751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/categories?post=5751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/tags?post=5751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}