{"id":422,"date":"2009-05-06T19:06:18","date_gmt":"2009-05-06T17:06:18","guid":{"rendered":"http:\/\/patchlog.com\/?p=422"},"modified":"2009-05-06T19:07:19","modified_gmt":"2009-05-06T17:07:19","slug":"comparing-float-values-in-php","status":"publish","type":"post","link":"https:\/\/patchlog.com\/php\/comparing-float-values-in-php\/","title":{"rendered":"Comparing float values in PHP"},"content":{"rendered":"<p>I had to learn this the hard way. It seems in php 27.64 is just not quite 27.64 , but something close to it ( that's what some people say here anyway: <a href=\"http:\/\/www.php.net\/ternary\">http:\/\/www.php.net\/ternary<\/a> ) .<\/p>\n<p>And that's why the following code wouldn't echo the text \"dude\" as you might think it should:<\/p>\n<pre><ol><li style=\"font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;\"><div style=\"font-family: 'Courier New', Courier, monospace; font-weight: normal;\">&nbsp;<\/div><\/li><\/ol><\/pre>\n<p>Run this script and you should see:<\/p>\n<pre>\r\namountdue: 27.64, amount: 27.64\r\nbut still no match\r\n<\/pre>\n<p>That's the dumbest thing I've ever seen. A similar code written in C gives the good results. Isn't php written in C? They should have copied C's good behavior.<br \/>\nIf conversion from float to string gives the right results, why not use the same results when comparing float values?<br \/>\nSo if you're having this problem it seems this will work: <\/p>\n<pre><ol><li style=\"font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;\"><div style=\"font-family: 'Courier New', Courier, monospace; font-weight: normal;\">&nbsp;<\/div><\/li><\/ol><\/pre>\n<p>The fact that php can't compare float values reliably and that you have to resort to this to make it work just makes php look bad. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had to learn this the hard way. It seems in php 27.64 is just not quite 27.64 , but something close to it ( that&#8217;s what some people say here anyway: http:\/\/www.php.net\/ternary ) . And that&#8217;s why the following code wouldn&#8217;t echo the text &#8220;dude&#8221; as you might think it should: &nbsp; Run this &hellip; <a href=\"https:\/\/patchlog.com\/php\/comparing-float-values-in-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Comparing float values in PHP<\/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":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8],"tags":[259,258,257,356],"class_list":["post-422","post","type-post","status-publish","format-standard","hentry","category-php","tag-bug","tag-comparison","tag-float","tag-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pofPh-6O","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/comments?post=422"}],"version-history":[{"count":3,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/422\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/422\/revisions\/425"}],"wp:attachment":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/media?parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/categories?post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/tags?post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}