{"id":1277,"date":"2012-06-14T06:19:42","date_gmt":"2012-06-14T06:19:42","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=1277"},"modified":"2012-06-14T06:19:42","modified_gmt":"2012-06-14T06:19:42","slug":"querying-float-fields-in-mysql","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2012\/06\/14\/querying-float-fields-in-mysql\/","title":{"rendered":"Querying float fields in mysql"},"content":{"rendered":"<p><a href=\"https:\/\/truelogic.org\/wordpress\/2012\/06\/14\/querying-float-fields-in-mysql\/pi\/\" rel=\"attachment wp-att-1279\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-1279\" title=\"pi\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2012\/06\/pi.jpeg\" alt=\"\" width=\"218\" height=\"231\" \/><\/a>Common mistake when querying float fields is to use the equal sign eg.<\/p>\n<p><em>select * from table where amt = 19.99<\/em><\/p>\n<p>This never works because the precision level is never going to be exact. So a better way is :<\/p>\n<p><em>select * from table<\/em><em> where abs(amt) &#8211; 19.99 &lt; 0.001<\/em><\/p>\n<p>You can change the precision level to something higher than 0.001 if you are doing scientific calculations but generally for money amounts, this precision is enough.<\/p>\n<p>Of course, if a field is supposed to store a currency value then its best to make it as decimal instead of float since a decimal field will automatically truncate the value to 2 significant digits after the decimal.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Common mistake when querying float fields is to use the equal sign eg. select * from table where amt = 19.99 This never works because <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2012\/06\/14\/querying-float-fields-in-mysql\/\" title=\"Querying float fields in mysql\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[278],"tags":[],"class_list":["post-1277","post","type-post","status-publish","format-standard","hentry","category-mysql-2"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=1277"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1277\/revisions"}],"predecessor-version":[{"id":1286,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1277\/revisions\/1286"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}