{"id":542,"date":"2017-07-08T18:31:15","date_gmt":"2017-07-08T22:31:15","guid":{"rendered":"http:\/\/www.amixa.com\/blog\/?p=542"},"modified":"2017-07-08T18:31:49","modified_gmt":"2017-07-08T22:31:49","slug":"sprintf-or-printf-do-not-output-float-variables-correctly","status":"publish","type":"post","link":"https:\/\/www.amixa.com\/blog\/2017\/07\/08\/sprintf-or-printf-do-not-output-float-variables-correctly\/","title":{"rendered":"sprintf or printf do not output float variables correctly"},"content":{"rendered":"<p>Have you run into situation under the MPLAB IDE where you are trying to output variables, but even though you have the format correct, a float variable outputs with a .0 value?<\/p>\n<p>Example<\/p>\n<pre>(from somewhere else in code)\r\nAIRTEMP=751\r\n====\r\nfloat adjustedtemp= 0.0;\r\nadjustedtemp= (AIRTEMP + 5)\/10;\r\nprintf(\"Air Temp: %5.2f \\r\\n\", adjustedtemp);\r\n\r\nthe result looks like this\r\n\r\nAir Temp: 75.00\r\n\r\nwhen it SHOULD look like this\r\n\r\nAir Temp: 75.60\r\n<\/pre>\n<p>this is due to a bug in the way MPLAB handles floats.<\/p>\n<p>Two easy ways to fix it:<\/p>\n<p>#1:<\/p>\n<pre>adjustedtemp= (AIRTEMP + 5.0)\/10.0;<\/pre>\n<p>or,<\/p>\n<p>#2<\/p>\n<pre>adjustedtemp= ((float)AIRTEMP + 5)\/10;<\/pre>\n<p>Either of these methods will result in the correct value being displayed during your sprintf() or printf() command.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you run into situation under the MPLAB IDE where you are trying to output variables, but even though you have the format correct, a float variable outputs with a .0 value? Example (from somewhere else in code) AIRTEMP=751 ==== float adjustedtemp= 0.0; adjustedtemp= (AIRTEMP + 5)\/10; printf(&#8220;Air Temp: %5.2f \\r\\n&#8221;, adjustedtemp); the result looks like this Air Temp: 75.00<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[129],"tags":[131,130],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-pic-xc8","tag-embedded-processor-programming","tag-pic-xc8"],"_links":{"self":[{"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":2,"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amixa.com\/blog\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}