{"id":5648,"date":"2014-11-28T09:00:35","date_gmt":"2014-11-28T14:00:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5648"},"modified":"2014-12-10T00:31:08","modified_gmt":"2014-12-10T05:31:08","slug":"rounding-digits","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/11\/28\/rounding-digits\/","title":{"rendered":"Rounding Digits"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/26212\"><tt>roundsd<\/tt><\/a> by <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/authors\/71979\">Fran\u00e7ois Beauducel<\/a>.\r\n   <\/p>\r\n   <p>There are a number of entries, including <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/21225\">this<\/a>, <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/4261\">this<\/a>, <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/6076\">this<\/a>, and <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/6077\">this<\/a>, that perform this useful task of rounding numbers to specific significant digits. Fran\u00e7ois provides a simple, well-implemented\r\n      function with good documentation. Here's how it works.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_rounddigits\/potw_rounddigits_eq12719111378361454579.png\">  rounded to 3 significant digits\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">long<\/span>\r\nroundsd(pi,3)<\/pre><pre style=\"font-style:oblique\">ans =\r\n   3.140000000000000\r\n<\/pre><div>\r\n      <ul>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_rounddigits\/potw_rounddigits_eq12719111378361454579.png\"> \/100 rounded to 3 significant digits:\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">roundsd(pi\/100,3)<\/pre><pre style=\"font-style:oblique\">ans =\r\n   0.031400000000000\r\n<\/pre><p>It works with arrays as well.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">roundsd([pi pi\/100],3)<\/pre><pre style=\"font-style:oblique\">ans =\r\n   3.140000000000000   0.031400000000000\r\n<\/pre><p>Did you know that in R2014b, the <a href=\"\"><tt>round<\/tt><\/a> function gives you the option to do the same thing? It provides the option of rounding to significant digits or to digits\r\n      respect to the decimal point.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Round respect to the decimal point<\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">round([pi pi\/100],3)<\/pre><pre style=\"font-style:oblique\">ans =\r\n   3.142000000000000   0.031000000000000\r\n<\/pre><div>\r\n      <ul>\r\n         <li>Round to significant digits<\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">round([pi pi\/100],3,<span style=\"color: #A020F0\">'significant'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Reset format<\/span>\r\nformat <span style=\"color: #A020F0\">short<\/span><\/pre><pre style=\"font-style:oblique\">ans =\r\n   3.140000000000000   0.031400000000000\r\n<\/pre><p>Thanks, Fran\u00e7ois (and others), for providing this functionality to folks using MATLAB versions prior to R2014b!<\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5648#respond\">here<\/a> or leave a <a href=\"http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/26212#comments\">comment<\/a> for Fran\u00e7ois.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_23e8a96fe8f642558ac0b77f0a37fc67() {\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='23e8a96fe8f642558ac0b77f0a37fc67 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 23e8a96fe8f642558ac0b77f0a37fc67';\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 = '';\r\n        copyright = 'Copyright 2014 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_23e8a96fe8f642558ac0b77f0a37fc67()\"><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; R2014b<br><\/p>\r\n<\/div>\r\n<!--\r\n23e8a96fe8f642558ac0b77f0a37fc67 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/26212 |roundsd|> by\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/authors\/71979\r\n% Fran\u00e7ois Beauducel>.\r\n%\r\n% There are a number of entries, including\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/21225 this>,\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/4261 this>,\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/6076 this>, and\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/6077 this>, that\r\n% perform this useful task of rounding numbers to specific significant\r\n% digits. Fran\u00e7ois provides a simple, well-implemented function with good\r\n% documentation. Here's how it works.\r\n%\r\n% * $\\pi$ rounded to 3 significant digits\r\n\r\nformat long\r\nroundsd(pi,3)\r\n\r\n%%\r\n% * $\\pi$\/100 rounded to 3 significant digits:\r\n\r\nroundsd(pi\/100,3)\r\n\r\n%%\r\n% It works with arrays as well.\r\n\r\nroundsd([pi pi\/100],3)\r\n\r\n%%\r\n% Did you know that in R2014b, the\r\n% <\r\n% |round|> function gives you the option to do the same thing? It provides\r\n% the option of rounding to significant digits or to digits respect to the\r\n% decimal point.\r\n%\r\n% * Round respect to the decimal point\r\n\r\nround([pi pi\/100],3)\r\n\r\n%%\r\n% * Round to significant digits\r\n\r\nround([pi pi\/100],3,'significant')\r\n\r\n% Reset format\r\nformat short\r\n\r\n%%\r\n% Thanks, Fran\u00e7ois (and others), for providing this functionality to folks\r\n% using MATLAB versions prior to R2014b!\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5648#respond here> or leave a\r\n% <http:\/\/jp.mathworks.com\/matlabcentral\/fileexchange\/26212#comments comment> for Fran\u00e7ois\r\n\r\n##### SOURCE END ##### 23e8a96fe8f642558ac0b77f0a37fc67\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/pick\/files\/roundsd_screenshot.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\n   Jiro's pick this week is roundsd by Fran\u00e7ois Beauducel.\r\n   \r\n   There are a number of entries, including this, this, this, and this, that perform this useful task of rounding numbers to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/11\/28\/rounding-digits\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":5738,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5648"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5648"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5648\/revisions"}],"predecessor-version":[{"id":5739,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5648\/revisions\/5739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media\/5738"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}