{"id":197,"date":"2009-09-03T14:38:27","date_gmt":"2009-09-03T14:38:27","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/03\/rounding-results\/"},"modified":"2018-06-11T11:19:35","modified_gmt":"2018-06-11T16:19:35","slug":"rounding-results","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/03\/rounding-results\/","title":{"rendered":"Rounding Results"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are frequent questions on the <a>MATLAB newsgroup<\/a> about rounding results to a certain number of decimal places.  MATLAB itself doesn't provide this functionality explicitly,\r\n         though it is easy to accomplish.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Sidetrack : A Little MathWorks History<\/a><\/li>\r\n         <li><a href=\"#2\">Example<\/a><\/li>\r\n         <li><a href=\"#7\">Tools for Rounding Solutions<\/a><\/li>\r\n         <li><a href=\"#9\">Question for You<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Sidetrack : A Little MathWorks History<a name=\"1\"><\/a><\/h3>\r\n   <p>MathWorks' first Massachusetts office phone number was 653-1415 (ignoring country and area codes). The astute reader will\r\n      notice that the last 5 digits are an approximation for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/197\/rounding_eq11731.png\">  (or, in MATLAB, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/pi.html\"><tt>pi<\/tt><\/a>). A local resident called one day to say that she kept getting calls for MathWorks and she wasn't sure why.  But it was quite\r\n      inconvenient for her because she spent lots of time on the second floor of her home, and the phone was on the first floor.\r\n       The excess round-trips were taxing her! To understand what was happening, you should know that in some of the early MathWorks\r\n      materials, the phone number was listed as <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/197\/rounding_eq41424.png\">  <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/197\/rounding_eq11731.png\"> .\r\n   <\/p>\r\n   <h3>Example<a name=\"2\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">long<\/span>\r\nx = 1.23456789<\/pre><pre style=\"font-style:oblique\">x =\r\n   1.234567890000000\r\n<\/pre><p>Use <tt>round<\/tt>. Here we get no decimals at all.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">round(x)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>There are many ways to get the number of decimals you want. Here's one way to round to 3 decimals.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">round(x*1000)\/1000<\/pre><pre style=\"font-style:oblique\">ans =\r\n   1.235000000000000\r\n<\/pre><p>Here's another way.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sprintf(<span style=\"color: #A020F0\">'%0.3f'<\/span>,x)<\/pre><pre style=\"font-style:oblique\">ans =\r\n1.235\r\n<\/pre><p>And another.  In this case, using the \"easy\" way to specify the format, you need to know how many integral digits there are\r\n      as well.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">str2num(num2str(x,4))<\/pre><pre style=\"font-style:oblique\">ans =\r\n   1.235000000000000\r\n<\/pre><h3>Tools for Rounding Solutions<a name=\"7\"><\/a><\/h3>\r\n   <p>There are a lot of tools for helping you round numbers.  The functions I list here for MATLAB form the basis of many, if not\r\n      all, of the specific solutions.\r\n   <\/p>\r\n   <p>MATLAB<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/round.html\"><tt>round<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/fix.html\"><tt>fix<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/floor.html\"><tt>floor<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/ceil.html\"><tt>ceil<\/tt><\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/sprintf.html\"><tt>sprintf<\/tt><\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Mapping Toolbox<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/map\/roundn.html\"><tt>roundn<\/tt><\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>File Exchange Rounding Tools<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=round+decimal\">Search : 'round decimal'<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24213-round2dp\">Solution: round to power<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23949-rounding-off-number-s--to-specified-significant-figures\">Solution: round to significant figures<\/a><\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% N= num2str(X,SF);<\/span>\r\n<span style=\"color: #228B22\">% N= str2num(N);<\/span><\/pre><h3>Question for You<a name=\"9\"><\/a><\/h3>\r\n   <p>When you round values, do you want this for display only, or wanted for calculations?  Some applications I can think of might\r\n      include processing data that has a smaller number of bits of precision to start with.  What applications do you need rounding\r\n      for in your calculations?  Post <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=197#respond\">here<\/a> with your thoughts.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_67a09b17dc6e4bee9bb4d02ab1aa4e1f() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='67a09b17dc6e4bee9bb4d02ab1aa4e1f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 67a09b17dc6e4bee9bb4d02ab1aa4e1f';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_67a09b17dc6e4bee9bb4d02ab1aa4e1f()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.8<br><\/p>\r\n<\/div>\r\n<!--\r\n67a09b17dc6e4bee9bb4d02ab1aa4e1f ##### SOURCE BEGIN #####\r\n%% Rounding Results\r\n% There are frequent questions on the\r\n% <http:\/\/ MATLAB newsgroup>\r\n% about rounding results to a certain number of decimal places.  MATLAB\r\n% itself doesn't provide this functionality explicitly, though it is easy\r\n% to accomplish.\r\n%% Sidetrack : A Little MathWorks History\r\n% MathWorks' first Massachusetts office phone number was 653-1415 (ignoring\r\n% country and area codes). The astute reader will notice that the last 5\r\n% digits are an approximation for $\\pi$ (or, in MATLAB,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/pi.html |pi|>).\r\n% A local resident called one day to say that she kept getting calls for\r\n% MathWorks and she wasn't sure why.  But it was quite inconvenient for her\r\n% because she spent lots of time on the second floor of her home, and the\r\n% phone was on the first floor.  The excess round-trips were taxing her! To\r\n% understand what was happening, you should know that in some of the early\r\n% MathWorks materials, the phone number was listed as $65$ $\\pi$.\r\n%% Example\r\nformat long\r\nx = 1.23456789\r\n%%\r\n% Use |round|. Here we get no decimals at all.\r\nround(x)\r\n%%\r\n% There are many ways to get the number of decimals you want. Here's one\r\n% way to round to 3 decimals.\r\nround(x*1000)\/1000\r\n%% \r\n% Here's another way.\r\nsprintf('%0.3f',x)\r\n%% \r\n% And another.  In this case, using the \"easy\" way to specify the format,\r\n% you need to know how many integral digits there are as well.\r\nstr2num(num2str(x,4))\r\n\r\n%% Tools for Rounding Solutions\r\n% There are a lot of tools for helping you round numbers.  The functions I\r\n% list here for MATLAB form the basis of many, if not all, of the specific\r\n% solutions.\r\n%%\r\n% MATLAB\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/round.html |round|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/fix.html |fix|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/floor.html |floor|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/ceil.html |ceil|>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/sprintf.html |sprintf|>\r\n%\r\n% Mapping Toolbox\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/map\/ref\/roundn.html |roundn|>\r\n%\r\n% File Exchange Rounding Tools\r\n%\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=round+decimal Search : 'round decimal'>\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24213-round2dp Solution: round to power>\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23949-rounding-off-number-s--to-specified-significant-figures Solution: round to significant figures>\r\n\r\n% N= num2str(X,SF);\r\n% N= str2num(N);\r\n\r\n%% Question for You\r\n% When you round values, do you want this for display only, or wanted for\r\n% calculations?  Some applications I can think of might include processing\r\n% data that has a smaller number of bits of precision to start with.  What\r\n% applications do you need rounding for in your calculations?  Post\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=197#respond here> with your\r\n% thoughts.\r\n\r\n##### SOURCE END ##### 67a09b17dc6e4bee9bb4d02ab1aa4e1f\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are frequent questions on the MATLAB newsgroup about rounding results to a certain number of decimal places.  MATLAB itself doesn't provide this functionality explicitly,\r\n        ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/03\/rounding-results\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[26,13],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/197"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":2944,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/197\/revisions\/2944"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}