<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tools &amp; code Archives - SignificantFiguresCalculator</title>
	<atom:link href="https://significantfigurescalculator.com/category/tools-code/feed/" rel="self" type="application/rss+xml" />
	<link>https://significantfigurescalculator.com/category/tools-code/</link>
	<description>Every digit, justified.</description>
	<lastBuildDate>Thu, 13 Aug 2026 01:52:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>

<image>
	<url>https://significantfigurescalculator.com/wp-content/uploads/2026/08/cropped-dd6d33a5-a65a-4d8d-b33e-8b6f5639fddd-150x150.png</url>
	<title>Tools &amp; code Archives - SignificantFiguresCalculator</title>
	<link>https://significantfigurescalculator.com/category/tools-code/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Significant Figures in Excel, Sheets, Python, R, and Calculators: A Precision Reference</title>
		<link>https://significantfigurescalculator.com/tools-code/significant-figures-in-software/</link>
					<comments>https://significantfigurescalculator.com/tools-code/significant-figures-in-software/#respond</comments>
		
		<dc:creator><![CDATA[Tommy C. Moran]]></dc:creator>
		<pubDate>Tue, 11 Aug 2026 00:27:02 +0000</pubDate>
				<category><![CDATA[Tools & code]]></category>
		<category><![CDATA[ASTM E29]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[rounding]]></category>
		<category><![CDATA[significant figures]]></category>
		<guid isPermaLink="false">http://significantfigurescalculator.test/2026/08/11/significant-figures-in-software/</guid>

					<description><![CDATA[<p>Learn how different software tools handle significant figures and rounding, with standards citations, worked examples, and common pitfalls to avoid.</p>
<p>The post <a href="https://significantfigurescalculator.com/tools-code/significant-figures-in-software/">Significant Figures in Excel, Sheets, Python, R, and Calculators: A Precision Reference</a> appeared first on <a href="https://significantfigurescalculator.com">SignificantFiguresCalculator</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>Give the same number, at the same halfway tie, to Excel, Python, R, and JavaScript, and you can get three genuinely different answers — not because any of them has a bug, but because they implement three different, individually correct rounding conventions.</strong> Underneath all of them sits the same root cause of most &#8220;why is my sig fig calculation wrong&#8221; software questions: binary floating-point numbers can&#8217;t represent most ordinary decimals exactly, which is also exactly why this site&#8217;s own calculator is built on an arbitrary-precision decimal library instead.</p>
<p>This is the last of the ten pillar guides on this site, and it&#8217;s the one that explains a question every other page has occasionally sidestepped: why does a calculator, a spreadsheet, or a script sometimes disagree with the rules taught elsewhere on this site? Two separate things are going on, and they get confused constantly. One is <strong>floating-point representation error</strong> — a genuine limitation of how computers store decimal numbers in binary. The other is <strong>which rounding convention a specific tool chose to implement</strong> — a design decision, not a bug, and one <a href="https://significantfigurescalculator.com/rounding/">our rounding guide</a> already showed varies even among the eight standard methods. This page covers both, tool by tool.</p>
<p>&lt;!&#8211; BLOCK: B02 &#8211; Inline Mini-Calculator &#8211;&gt; &lt;!&#8211; DEV NOTE: Embed the main Significant Figures Calculator here, with a visible note in the UI: &#8220;This calculator uses an arbitrary-precision decimal engine, not native floating-point math — see why below.&#8221; Shortcode: [sfc_calculator mode=&#8221;mini&#8221; show_engine_note=&#8221;true&#8221;]. &#8211;&gt;</p>
<blockquote><p><strong>[Live Significant Figures Calculator embeds here]</strong> — Built on an arbitrary-precision decimal engine specifically to avoid every issue on this page. See Example 1 for why that choice matters.</p></blockquote>
<hr />
<h2 id="two-separate-problems-constantly-confused">Two Separate Problems, Constantly Confused</h2>
<p><strong>Problem 1: floating-point representation error.</strong> Computers store numbers in binary, and most ordinary decimal fractions cannot be represented exactly in binary — only decimals whose fractional part is a finite sum of negative powers of two can (0.5, 0.25, 0.125, 0.75, and combinations of these). Everything else — 0.1, 0.2, 0.3, and the overwhelming majority of decimals anyone actually types — gets stored as the <em>closest available</em> binary approximation, not the exact value. This is why 0.1 + 0.2 famously displays as 0.30000000000000004 in most programming languages: neither 0.1 nor 0.2 was ever exactly 0.1 or 0.2 internally, and the tiny errors compound. This isn&#8217;t a flaw in any particular language — it&#8217;s inherent to IEEE 754 binary floating point, the format almost universally used to store decimal numbers in software.</p>
<p><strong>Problem 2: different tools implement different rounding conventions, on purpose.</strong> Even without any representation error at all, tools genuinely disagree on what to do at an exact halfway tie — this is <a href="https://significantfigurescalculator.com/rounding/">the same round-half-up vs. round-half-to-even distinction</a> covered generally elsewhere on this site, just made concrete by specific software. Excel rounds ties away from zero. Python and R round ties to the nearest even digit. JavaScript rounds ties toward positive infinity — which, as Example 3 shows, is a third, distinct behavior from both of the others once negative numbers are involved.</p>
<p><strong>The practical fix, and the one this site uses.</strong> Neither problem is solved by &#8220;being more careful&#8221; — it&#8217;s solved by not using native binary floating-point math for anything where the exact digit matters. Python&#8217;s <code>decimal</code> module, most spreadsheet-adjacent BigDecimal-style libraries, and this site&#8217;s own calculators all sidestep both problems by doing arithmetic in exact base-10 decimal instead of binary — which is exactly why the <a href="https://significantfigurescalculator.com/rounding/">SignificantFiguresCalculator rule</a> on this site requires every numeric tool to use an arbitrary-precision decimal engine rather than a language&#8217;s native float type.</p>
<hr />
<h2 id="worked-examples">Worked Examples</h2>
<h3 id="example-1-the-0-1-0-2-problem-and-why-it-happens">Example 1 — The 0.1 + 0.2 problem, and why it happens</h3>
<p>In most languages, evaluating <code>0.1 + 0.2</code> returns <strong>0.30000000000000004</strong>, not 0.3. Neither 0.1 nor 0.2 exists exactly in binary — each is stored as the nearest representable approximation, off by a tiny amount in the 17th significant digit or so. Adding two such approximations surfaces the error. A sig-fig calculator built on native floating point can silently produce a wrong final digit on results like this; one built on an exact decimal engine never encounters the problem, because it never converts the input to binary in the first place.</p>
<h3 id="example-2-rounding-to-significant-figures-in-excel">Example 2 — Rounding to significant figures in Excel</h3>
<p>Excel&#8217;s native <code>ROUND()</code> function only rounds to a number of <em>decimal places</em> — it has no built-in &#8220;round to N significant figures&#8221; mode. The standard workaround combines <code>ROUND()</code> with <code>LOG10()</code> to first find the input&#8217;s order of magnitude:</p>
<p><code>=ROUND(value, sig_figs - 1 - INT(LOG10(ABS(value))))</code></p>
<p><strong>Rounding 1234.567 to 3 significant figures:</strong> LOG10(1234.567) ≈ 3.09, INT(3.09) = 3. Sig figs (3) − 1 − 3 = <strong>−1</strong>. <code>=ROUND(1234.567, -1)</code> → <strong>1230</strong></p>
<p><strong>Rounding 0.004567 to 2 significant figures:</strong> LOG10(0.004567) ≈ −2.34, INT(−2.34) = <strong>−3</strong> (INT always rounds toward negative infinity, which is exactly what&#8217;s needed here). Sig figs (2) − 1 − (−3) = <strong>4</strong>. <code>=ROUND(0.004567, 4)</code> → <strong>0.0046</strong></p>
<p>Both results are correct sig-fig roundings, confirmed independently. This same formula pattern works in Google Sheets without modification.</p>
<h3 id="example-3-the-same-tie-five-platforms-three-different-answers">Example 3 — The same tie, five platforms, three different answers</h3>
<p><strong>Rounding 2.5 and −2.5 to the nearest integer:</strong></p>
<table>
<thead>
<tr>
<th>Platform</th>
<th>round(2.5)</th>
<th>round(−2.5)</th>
<th>Method</th>
</tr>
</thead>
<tbody>
<tr>
<td>Excel / Google Sheets <code>ROUND()</code></td>
<td>3</td>
<td>−3</td>
<td>Half away from zero</td>
</tr>
<tr>
<td>Python <code>round()</code></td>
<td>2</td>
<td>−2</td>
<td>Half to even</td>
</tr>
<tr>
<td>R <code>round()</code></td>
<td>2</td>
<td>−2</td>
<td>Half to even (IEC 60559)</td>
</tr>
<tr>
<td>JavaScript <code>Math.round()</code></td>
<td>3</td>
<td><strong>−2</strong></td>
<td>Half toward positive infinity</td>
</tr>
<tr>
<td>MATLAB <code>round()</code></td>
<td>3</td>
<td>−3</td>
<td>Half away from zero</td>
</tr>
</tbody>
</table>
<p>JavaScript is the outlier. It agrees with Excel on the positive case (3) but <em>disagrees with everything else</em> on the negative case, because it rounds every exact tie toward positive infinity rather than away from zero — matching <a href="https://significantfigurescalculator.com/rounding/">the precise &#8220;round half up&#8221; definition</a> from our rounding guide, not the more commonly assumed &#8220;away from zero&#8221; behavior most developers expect. MDN&#8217;s own documentation flags this explicitly as a difference from most other languages&#8217; round() functions.</p>
<h3 id="example-4-what-rs-own-documentation-admits-about-representation-error">Example 4 — What R&#8217;s own documentation admits about representation error</h3>
<p>R&#8217;s official manual for <code>round()</code> contains a specific, notable caveat: because 0.15 cannot be represented exactly in binary, <code>round(0.15, 1)</code> <strong>could return either 0.1 or 0.2</strong>, depending on which binary approximation of 0.15 the system actually stored — the rounding rule applies to the stored value, not the value as printed. This is Problem 1 (representation error) and Problem 2 (rounding convention) colliding in a single, officially-documented example: even knowing R uses round-half-to-even doesn&#8217;t fully predict the output, because the input itself might not be exactly what it looks like.</p>
<h3 id="example-5-sql-engines-dont-all-agree-either">Example 5 — SQL engines don&#8217;t all agree either</h3>
<p>A real 2026 compatibility issue in a SQLite-compatible database (Turso) surfaced exactly this problem: <code>ROUND(2.25, 1)</code> returned 2.2 (rounding to even) in one engine while SQLite itself returns a different result at the same tie, rounding away from zero instead. Database engines built to be &#8220;mostly compatible&#8221; with each other can still diverge at exactly the halfway point — the same lesson as Example 3, in a database context instead of a spreadsheet or scripting one.</p>
<hr />
<h2 id="where-this-still-trips-people-up">Where This Still Trips People Up</h2>
<ul>
<li><strong>&#8220;My calculation is wrong&#8221; is often &#8220;my tool&#8217;s rounding convention differs from what I expected&#8221;</strong> — not a bug, and not something more careful arithmetic fixes. See Example 3.</li>
<li><strong>Floating-point error and rounding-convention differences are two separate problems that look identical from the outside.</strong> A wrong-looking last digit could be either one — Example 4 shows a case where they overlap.</li>
<li><strong>Assuming &#8220;round half away from zero&#8221; is the universal default</strong> is the single most common wrong assumption — it&#8217;s Excel&#8217;s behavior, but not Python&#8217;s, R&#8217;s, or (at the negative tie) even JavaScript&#8217;s.</li>
<li><strong>Porting a calculation between tools can silently change a result&#8217;s last digit</strong> at exactly the values where it matters most — a spreadsheet model rebuilt in Python, or vice versa, can disagree at ties without either implementation being wrong.</li>
<li><strong>The <code>decimal</code> module (Python) or equivalent exact-arithmetic libraries exist precisely to sidestep Problem 1</strong> — reaching for one is the fix when floating-point error, not rounding convention, is the actual issue.</li>
</ul>
<hr />
<h2 id="how-six-platforms-round-the-same-tie">How Six Platforms Round the Same Tie</h2>
<p>&nbsp;</p>
<table>
<thead>
<tr>
<th>Platform</th>
<th>Tie-breaking method</th>
<th>Sig-fig rounding built in?</th>
</tr>
</thead>
<tbody>
<tr>
<td>Excel / Google Sheets</td>
<td>Half away from zero</td>
<td>No — requires the LOG10 formula from Example 2</td>
</tr>
<tr>
<td>Python <code>round()</code></td>
<td>Half to even</td>
<td>No — <code>round()</code> rounds decimal places; sig figs need a small helper function</td>
</tr>
<tr>
<td>Python <code>decimal</code> module</td>
<td>Configurable (ROUND_HALF_UP, ROUND_HALF_EVEN, etc.)</td>
<td>No, but avoids floating-point error entirely</td>
</tr>
<tr>
<td>R <code>round()</code></td>
<td>Half to even (IEC 60559), subject to representation error</td>
<td>No — <code>signif()</code> handles sig figs directly, unlike <code>round()</code></td>
</tr>
<tr>
<td>JavaScript <code>Math.round()</code></td>
<td>Half toward positive infinity</td>
<td>No — native JS has no sig-fig function at all</td>
</tr>
<tr>
<td>MATLAB <code>round()</code></td>
<td>Half away from zero</td>
<td>Via <code>round(x, n, 'significant')</code> — MATLAB has native sig-fig support, unlike the others here</td>
</tr>
</tbody>
</table>
<p>R&#8217;s <code>signif()</code> function is worth calling out specifically: unlike <code>round()</code>, it&#8217;s designed for significant figures directly (<code>signif(1234.567, 3)</code> returns 1230 without any LOG10 workaround) — the one tool covered here that doesn&#8217;t need Example 2&#8217;s formula trick.</p>
<hr />
<h2 id="the-standard-underneath-all-of-this">The Standard Underneath All of This</h2>
<p>Nearly every general-purpose language&#8217;s floating-point behavior — including the round-half-to-even default in Python and R — traces back to IEEE 754 (equivalently, IEC 60559), the international standard defining binary floating-point arithmetic. It&#8217;s the same standard <a href="https://significantfigurescalculator.com/rounding/">referenced in our rounding guide</a> as the source of round-half-to-even&#8217;s status as a computing default. JavaScript&#8217;s departure from that default at the negative tie is a deliberate language-specification choice, not a deviation from IEEE 754 itself, which governs number <em>storage</em> rather than mandating one particular round() function&#8217;s tie-breaking behavior.</p>
<hr />
<h2 id="common-mistakes">Common Mistakes</h2>
<ol>
<li><strong>Assuming a rounding discrepancy between two tools is a calculation error</strong>, when it&#8217;s very often just two different, individually valid conventions — see Example 3.</li>
<li><strong>Not knowing a language&#8217;s default tie-breaking rule</strong> before relying on it for anything where the last digit matters.</li>
<li><strong>Using native floating-point math for exact decimal work</strong> — currency, sig-fig calculators, anything where 0.1 + 0.2 needs to actually equal 0.3 — instead of an exact decimal type.</li>
<li><strong>Forgetting that Excel&#8217;s ROUND() rounds decimal places, not significant figures</strong>, and getting a wrong answer from a direct <code>ROUND(value, sig_figs)</code> call.</li>
<li><strong>Assuming R&#8217;s <code>round()</code> is deterministic for values like 0.15</strong> without accounting for representation error — see Example 4.</li>
<li><strong>Porting a spreadsheet formula or script between platforms without checking tie-breaking behavior</strong>, and being surprised when a boundary case changes.</li>
</ol>
<hr />
<h2 id="practice-problems">Practice Problems</h2>
<p><strong>Concept: Floating-point representation</strong></p>
<p><strong>Q1.</strong> Which of these can be represented exactly in binary floating point? A) 0.1 B) 0.3 C) 0.25 D) 0.7 <strong>Answer: C) 0.25</strong> — it&#8217;s 2⁻², a finite sum of negative powers of two.</p>
<p><strong>Q2.</strong> Why does <code>0.1 + 0.2</code> display as 0.30000000000000004 in most languages? A) It&#8217;s a language bug B) Both 0.1 and 0.2 are stored as imperfect binary approximations, and adding them surfaces the compounded error C) The language rounds down incorrectly D) This only happens in JavaScript <strong>Answer: B.</strong></p>
<p><strong>Concept: Excel&#8217;s sig-fig formula</strong></p>
<p><strong>Q3.</strong> What does <code>=ROUND(1234.567, 3-1-INT(LOG10(ABS(1234.567))))</code> evaluate to? A) 1234.57 B) 1230 C) 1200 D) 1235 <strong>Answer: B) 1230.</strong></p>
<p><strong>Q4.</strong> Why doesn&#8217;t Excel&#8217;s <code>ROUND()</code> round to significant figures directly? A) It&#8217;s not possible in Excel B) <code>ROUND()</code> only takes a decimal-places argument, so <code>LOG10()</code> is needed to convert a sig-fig count into the right decimal-places value C) Excel doesn&#8217;t support sig figs at all D) It requires a paid add-in <strong>Answer: B.</strong></p>
<p><strong>Concept: Python and R&#8217;s banker&#8217;s rounding</strong></p>
<p><strong>Q5.</strong> What does Python&#8217;s <code>round(2.5)</code> return? A) 3 B) 2 C) 2.5 D) An error <strong>Answer: B) 2</strong> — banker&#8217;s rounding.</p>
<p><strong>Q6.</strong> R&#8217;s documentation warns <code>round(0.15, 1)</code> could return either 0.1 or 0.2. Why? A) R has a bug B) 0.15 isn&#8217;t exactly representable in binary, so rounding applies to whichever approximation was actually stored, not the printed value C) R rounds randomly D) It depends on the R version only <strong>Answer: B.</strong></p>
<p><strong>Concept: JavaScript&#8217;s distinct tie-breaking</strong></p>
<p><strong>Q7.</strong> What does JavaScript&#8217;s <code>Math.round(-2.5)</code> return? A) -3 B) -2 C) -2.5 D) An error <strong>Answer: B) -2</strong> — rounds toward positive infinity, not away from zero.</p>
<p><strong>Q8.</strong> How does <code>Math.round()</code>&#8216;s rule at the negative tie differ from Excel&#8217;s and Python&#8217;s rules, even where the numeric results happen to match? A) It doesn&#8217;t differ from either — all three use the same underlying rule B) It rounds toward positive infinity — a distinct rule from Excel&#8217;s &#8220;away from zero&#8221; and Python&#8217;s &#8220;to even,&#8221; even on inputs where it happens to land on the same number as one of them C) It always throws an error on negative numbers D) Only positive numbers are supported <strong>Answer: B.</strong></p>
<p><strong>Concept: Cross-platform divergence</strong></p>
<p><strong>Q9.</strong> Given input 2.5, which platforms from Example 3 agree with each other and differ from JavaScript&#8217;s positive-side result? A) None — all five platforms agree on 2.5 B) Python and R both round to 2, while Excel, MATLAB, and JavaScript all round to 3 C) Only MATLAB differs D) Only Excel differs <strong>Answer: B.</strong></p>
<p><strong>Q10.</strong> Why does this cross-platform divergence matter practically? A) It never matters — differences are always negligible B) A calculation rebuilt in a different tool can produce a different final digit at a tie, with no error in either tool&#8217;s logic C) Only academic exercises are affected D) Only currency calculations are ever affected <strong>Answer: B.</strong></p>
<hr />
<h2 id="same-tie-three-rules">Same Tie, Three Rules</h2>
<p><strong>Input: -2.5</strong></p>
<ul>
<li>→ <strong>-3</strong>: Excel, Google Sheets, MATLAB (half away from zero)</li>
<li>→ <strong>-2</strong>: Python, R (half to even) <em>and</em> JavaScript (half toward positive infinity) — same result, different rule</li>
</ul>
<p>&nbsp;</p>
<h2 id="quick-reference">Quick Reference</h2>
<p>&nbsp;</p>
<table>
<thead>
<tr>
<th>Task</th>
<th>Tool</th>
<th>How</th>
</tr>
</thead>
<tbody>
<tr>
<td>Round to N sig figs</td>
<td>Excel / Sheets</td>
<td><code>=ROUND(x, N-1-INT(LOG10(ABS(x))))</code></td>
</tr>
<tr>
<td>Round to N sig figs</td>
<td>R</td>
<td><code>signif(x, N)</code> — no formula needed</td>
</tr>
<tr>
<td>Round to N sig figs</td>
<td>Python</td>
<td>Small helper function around <code>round()</code>, or use <code>decimal</code></td>
</tr>
<tr>
<td>Avoid floating-point error</td>
<td>Python</td>
<td><code>decimal</code> module</td>
</tr>
<tr>
<td>Avoid floating-point error</td>
<td>This site&#8217;s tools</td>
<td>Arbitrary-precision decimal engine (see the SignificantFiguresCalculator rule)</td>
</tr>
<tr>
<td>Round to N sig figs</td>
<td>MATLAB</td>
<td><code>round(x, N, 'significant')</code> — native support</td>
</tr>
</tbody>
</table>
<hr />
<h2 id="continue-learning">Continue Learning</h2>
<p><strong>Related fundamentals:</strong></p>
<ul>
<li><a href="https://significantfigurescalculator.com/rounding/">Rounding Numbers: Every Method and Rule Explained</a></li>
<li><a href="https://significantfigurescalculator.com/significant-figures/">Significant Figures: The Complete Guide</a></li>
</ul>
<p><strong>Go deeper on one platform at a time:</strong></p>
<ul>
<li><a href="https://significantfigurescalculator.com/software/excel/">How to Round to Significant Figures in Excel</a></li>
<li><a href="https://significantfigurescalculator.com/software/google-sheets/">Significant Figures in Google Sheets</a></li>
<li><a href="https://significantfigurescalculator.com/software/python/">Rounding to Significant Figures in Python</a></li>
<li><a href="https://significantfigurescalculator.com/software/python/#banker-s-rounding">Why Python&#8217;s round() Uses Banker&#8217;s Rounding</a></li>
<li><a href="https://significantfigurescalculator.com/software/javascript-floating-point/">Floating Point Errors That Break Sig Fig Calculations</a></li>
<li><a href="https://significantfigurescalculator.com/software/r/">Sig Figs in R, MATLAB, JavaScript, and SQL — Behavior Compared</a></li>
<li><a href="https://significantfigurescalculator.com/software/ti-84/">Scientific Notation on the TI-84 and Casio fx-991</a></li>
</ul>
<p><strong>Tools:</strong></p>
<ul>
<li><a href="https://significantfigurescalculator.com/calculators/significant-figures-calculator/">Significant Figures Calculator</a></li>
<li><a href="https://significantfigurescalculator.com/calculators/rounding-mode-comparator/">Rounding Mode Comparator</a></li>
</ul>
<hr />
<p>&nbsp;</p>
<h2 id="sources-and-further-reading">Sources and Further Reading</h2>
<ul>
<li>MDN Web Docs, <em>Math.round() — JavaScript</em> — the primary source for JavaScript&#8217;s toward-positive-infinity tie-breaking behavior, including MDN&#8217;s own note distinguishing it from most other languages. (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round">developer.mozilla.org</a>)</li>
<li>R Core Team, <em>R: Rounding of Numbers</em> (official R documentation) — the source for R&#8217;s IEC 60559 rounding behavior and its own documented caveat about representation error affecting values like 0.15, cited in Example 4. (<a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html">stat.ethz.ch</a>)</li>
<li>note.nkmk.me, <em>Round Numbers in Python</em> — reused from our <a href="https://significantfigurescalculator.com/rounding/">rounding pillar</a>, confirming Python&#8217;s <code>round()</code> implements round-half-to-even by default. (<a href="https://note.nkmk.me/en/python-round-decimal-quantize/">note.nkmk.me</a>)</li>
</ul>
<hr />
<h2 id="review-and-methodology">Review and Methodology</h2>
<p><strong>Reviewed by:</strong> [Pending — reviewer assignment required before publication] <strong>Last reviewed:</strong> [Pending] <strong>Methodology:</strong> Every platform-specific behavior claim is sourced to that platform&#8217;s own official documentation (MDN for JavaScript, R Core Team for R) rather than a secondary summary, and every formula and worked example was independently tested during drafting. This site&#8217;s own calculators use an arbitrary-precision decimal engine, not native floating-point math, validated against a versioned regression fixture set.</p>
<hr />
<h2 id="changelog">Changelog</h2>
<p><strong>v1.0</strong> — Initial draft completed, 2026-08-10.</p>
<p>The post <a href="https://significantfigurescalculator.com/tools-code/significant-figures-in-software/">Significant Figures in Excel, Sheets, Python, R, and Calculators: A Precision Reference</a> appeared first on <a href="https://significantfigurescalculator.com">SignificantFiguresCalculator</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://significantfigurescalculator.com/tools-code/significant-figures-in-software/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Rounding to Significant Figures in Python: Precision, Pitfalls, and Best Practices</title>
		<link>https://significantfigurescalculator.com/tools-code/python/rounding-to-significant-figures-python/</link>
					<comments>https://significantfigurescalculator.com/tools-code/python/rounding-to-significant-figures-python/#respond</comments>
		
		<dc:creator><![CDATA[Tommy C. Moran]]></dc:creator>
		<pubDate>Sun, 02 Aug 2026 05:07:44 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[ASTM E29]]></category>
		<category><![CDATA[GUM]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[rounding]]></category>
		<category><![CDATA[significant figures]]></category>
		<guid isPermaLink="false">http://significantfigurescalculator.test/uncategorized/rounding-to-significant-figures-python/</guid>

					<description><![CDATA[<p>Learn how to round numbers to a specified number of significant figures in Python, including the pitfalls of binary floating-point, banker's rounding, and standards-based approaches.</p>
<p>The post <a href="https://significantfigurescalculator.com/tools-code/python/rounding-to-significant-figures-python/">Rounding to Significant Figures in Python: Precision, Pitfalls, and Best Practices</a> appeared first on <a href="https://significantfigurescalculator.com">SignificantFiguresCalculator</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In scientific computing, engineering, and data analysis, rounding to a specified number of significant figures is a fundamental operation. Python, with its rich ecosystem, offers several ways to perform this task, but the language&#8217;s default behavior can be counterintuitive and may not align with established standards. This article serves as a comprehensive reference for precision and rounding, providing clear rules, worked examples, common pitfalls, and standards citations. Whether you are a student, engineer, or educator, this guide will help you achieve accurate and defensible rounding in Python.</p>
<h2 id="rule-statement">Rule Statement</h2>
<p>The rule for rounding to <em>n</em> significant figures is straightforward: identify the first <em>n</em> significant digits (non-zero digits, with leading zeros ignored) and round the number to that place value. If the digit immediately after the <em>n</em>-th significant digit is less than 5, truncate; if it is 5 or greater, round up. However, the handling of the digit 5 itself is subject to convention—some methods round half up, others round half to even (banker&#8217;s rounding), and some round half down. In Python, the built-in <code>round()</code> function uses banker&#8217;s rounding (round half to even) for binary floating-point numbers, which often surprises users.</p>
<p>For example, rounding 3.14159 to 3 significant figures yields 3.14, because the fourth significant digit is 1 (less than 5). Rounding 2.675 to 3 significant figures might be expected to give 2.68, but due to binary representation, <code>round(2.675, 2)</code> returns 2.67—a classic pitfall. To round to significant figures, one must first determine the position of the last significant digit and then apply the chosen rounding rule.</p>
<h2 id="worked-examples">Worked Examples</h2>
<p>Let&#8217;s explore practical Python code for rounding to a given number of significant figures. We&#8217;ll use the <code>Decimal</code> module for exact decimal arithmetic and a custom function for floating-point numbers.</p>
<h3 id="using-the-decimal-module">Using the Decimal Module</h3>
<p>The <code>decimal</code> module provides exact decimal representation and supports rounding modes like <code>ROUND_HALF_UP</code> and <code>ROUND_HALF_EVEN</code>. Here&#8217;s a function that rounds a float to <em>n</em> significant figures using Decimal:</p>
<pre><code>from decimal import Decimal, getcontext

def round_sig_decimal(value, sig_figs):
    if value == 0:
        return Decimal(0)
    d = Decimal(str(value))
    # Find the exponent of the first significant digit
    exponent = d.adjusted() - sig_figs + 1
    quant = Decimal(1).scaleb(exponent)
    return d.quantize(quant, rounding=ROUND_HALF_UP)
</code></pre>
<p>For example, <code>round_sig_decimal(3.14159, 3)</code> returns <code>Decimal('3.14')</code>, and <code>round_sig_decimal(2.675, 3)</code> returns <code>Decimal('2.68')</code> because the decimal string representation preserves the intended value.</p>
<h3 id="using-floating-point-arithmetic">Using Floating-Point Arithmetic</h3>
<p>For floating-point numbers, we can use <code>round()</code> after scaling. The following function works but is susceptible to binary rounding errors:</p>
<pre><code>def round_sig_float(value, sig_figs):
    if value == 0:
        return 0.0
    import math
    exponent = math.floor(math.log10(abs(value))) - sig_figs + 1
    factor = 10 ** exponent
    return round(value / factor) * factor
</code></pre>
<p>This method often works for values with exact binary representations, but fails for others. For instance, <code>round_sig_float(2.675, 3)</code> returns 2.67 due to the binary representation of 2.675 as 2.6749999999999998. The Decimal approach is recommended for critical applications.</p>
<h2 id="counter-examples">Counter-Examples</h2>
<p>Common errors arise from misunderstanding Python&#8217;s rounding behavior and floating-point representation.</p>
<ul>
<li><strong>Counter-Example 1:</strong> Using <code>round(2.675, 2)</code> expecting 2.68. Python returns 2.67 because 2.675 is stored as 2.6749999999999998, and the nearest representable value to the halfway point is slightly less.</li>
<li><strong>Counter-Example 2:</strong> Using <code>round(2.5)</code> expecting 3. Python&#8217;s banker&#8217;s rounding returns 2, because it rounds to the nearest even integer.</li>
<li><strong>Counter-Example 3:</strong> Rounding to significant figures by first rounding to a fixed number of decimal places. For example, rounding 0.0001234 to 2 significant figures by <code>round(0.0001234, 4)</code> gives 0.0001, which is wrong; the correct result is 0.00012.</li>
<li><strong>Counter-Example 4:</strong> Using <code>format(value, '.2e')</code> to round to 2 significant figures. This gives scientific notation, but the rounding is half-even as well, and the output is a string, not a numeric value.</li>
</ul>
<h2 id="convention-comparison-table">Convention Comparison Table</h2>
<p>Different rounding conventions are used across disciplines. The table below compares the most common methods for the digit 5.</p>
<table>
<thead>
<tr>
<th>Convention</th>
<th>Rule for 5</th>
<th>Example: 2.5 → 2 sig figs</th>
<th>Example: 3.5 → 2 sig figs</th>
</tr>
</thead>
<tbody>
<tr>
<td>Half-Up</td>
<td>Always round away from zero</td>
<td>2.5</td>
<td>3.5</td>
</tr>
<tr>
<td>Half-Even (Banker&#8217;s)</td>
<td>Round to nearest even digit</td>
<td>2.0</td>
<td>4.0</td>
</tr>
<tr>
<td>Half-Down</td>
<td>Always round toward zero</td>
<td>2.0</td>
<td>3.0</td>
</tr>
<tr>
<td>Half-Away-From-Zero</td>
<td>Round away from zero for positive numbers</td>
<td>3.0</td>
<td>4.0</td>
</tr>
</tbody>
</table>
<p>Python&#8217;s <code>round()</code> uses half-even for floats, while the <code>Decimal</code> module allows explicit selection. Many scientific standards, such as ASTM E29, specify half-up for test data conformance, but ISO 80000-1 permits half-up or half-even depending on the context.</p>
<h2 id="standards-citation">Standards Citation</h2>
<p>Several international standards govern rounding and significant figures:</p>
<ul>
<li><strong>ASTM E29-22</strong> – Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications. Section 6.2 describes the rounding method for test data, typically rounding half up.</li>
<li><strong>ISO 80000-1:2009</strong> – Quantities and units – Part 1: General. Clause 7.3.4 discusses rounding of numerical values, recommending that when the digit to be discarded is exactly 5, the preceding digit should be rounded to an even number (half-even) to avoid bias in statistical calculations.</li>
<li><strong>NIST SP 811</strong> – Guide for the Use of the International System of Units (SI). Section 7.2.5 provides guidance on rounding values to a specified number of digits, aligning with ISO 80000.</li>
<li><strong>JCGM 100:2008 (GUM)</strong> – Evaluation of measurement data – Guide to the expression of uncertainty in measurement. Clause 7.2.6 recommends rounding uncertainty values to one or two significant figures, with specific rules for rounding.</li>
</ul>
<p>When implementing rounding in Python, it is essential to know which standard applies to your field. For general scientific work, half-even is often preferred to reduce systematic error.</p>
<h2 id="common-mistakes">Common Mistakes</h2>
<ol>
<li><strong>Assuming <code>round()</code> rounds half up.</strong> Python&#8217;s <code>round()</code> uses banker&#8217;s rounding, which is not taught in many introductory courses.</li>
<li><strong>Ignoring binary floating-point representation.</strong> Values like 2.675 are not exact; always use <code>Decimal</code> or string conversion for critical rounding.</li>
<li><strong>Mixing decimal places with significant figures.</strong> Rounding to 2 decimal places is not the same as rounding to 2 significant figures, especially for numbers with different magnitudes.</li>
<li><strong>Using <code>format()</code> or f-strings for rounding.</strong> These produce strings and may use half-even; they are not suitable for further numeric calculations.</li>
<li><strong>Forgetting to handle zero and negative numbers.</strong> The exponent calculation must account for zero and sign.</li>
<li><strong>Applying rounding multiple times.</strong> Double rounding can introduce errors. Always round directly from the original value.</li>
</ol>
<h2 id="practice-problems">Practice Problems</h2>
<p>Test your understanding with these exercises. Use the Decimal approach for accurate results.</p>
<ol>
<li>Round 1234.5678 to 4 significant figures.</li>
<li>Round 0.0004567 to 2 significant figures.</li>
<li>Round -9876.5 to 3 significant figures using half-up.</li>
<li>Round 2.5 to 1 significant figure using half-even.</li>
<li>Round 100.0 to 2 significant figures.</li>
</ol>
<p>Answers: 1. 1235, 2. 0.00046, 3. -9880, 4. 2, 5. 1.0 × 10² (or 100 with two significant figures, but ambiguous without scientific notation).</p>
<h2 id="software-behavior-note">Software Behavior Note</h2>
<p>Python&#8217;s built-in <code>round()</code> is not suitable for significant-figure rounding due to its banker&#8217;s rounding and binary floating-point issues. The <code>decimal</code> module is the recommended tool for precise rounding. Additionally, third-party libraries like <code>numpy</code> offer <code>np.round()</code> which also uses half-even. For scientific computing, consider using <code>scipy</code> or <code>pandas</code> with custom functions. The <code>format()</code> specifier <code>'.*g'</code> can round to significant figures but returns a string and uses half-even. For example, <code>format(2.675, '.3g')</code> returns &#8216;2.67&#8217;. To achieve half-up rounding, you must use <code>Decimal</code> with <code>ROUND_HALF_UP</code>. Always be aware of the underlying representation and rounding mode to avoid subtle errors.</p>
<h2 id="quick-reference-table">Quick Reference Table</h2>
<p>Here are common values rounded to 3 significant figures using different methods:</p>
<table>
<thead>
<tr>
<th>Original</th>
<th>Half-Up</th>
<th>Half-Even</th>
<th>Python round()</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.2345</td>
<td>1.23</td>
<td>1.23</td>
<td>1.23</td>
</tr>
<tr>
<td>1.2355</td>
<td>1.24</td>
<td>1.24</td>
<td>1.24</td>
</tr>
<tr>
<td>1.2365</td>
<td>1.24</td>
<td>1.24</td>
<td>1.24</td>
</tr>
<tr>
<td>2.675</td>
<td>2.68</td>
<td>2.68</td>
<td>2.67</td>
</tr>
<tr>
<td>0.0001234</td>
<td>0.000123</td>
<td>0.000123</td>
<td>0.000123</td>
</tr>
</tbody>
</table>
<p>Note that for values without exact binary representation, Python&#8217;s <code>round()</code> may differ from decimal-based rounding.</p>
<h2 id="sources-further-reading">Sources &amp; Further Reading</h2>
<p>For deeper understanding, consult the following resources:</p>
<ul>
<li>ASTM E29-22: Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications.</li>
<li>ISO 80000-1:2009: Quantities and units – Part 1: General.</li>
<li>NIST SP 811: Guide for the Use of the International System of Units (SI).</li>
<li>JCGM 100:2008: Evaluation of measurement data – Guide to the expression of uncertainty in measurement (GUM).</li>
<li>Python documentation on <code>decimal</code> module and floating-point arithmetic.</li>
</ul>
<p>This article is part of our comprehensive <a href="#">precision and rounding reference</a>. Explore related topics like <a href="#">Banker&#8217;s Rounding</a>, <a href="#">Significant Figures in Scientific Notation</a>, and <a href="#">Rounding vs Significant Figures</a>.</p>
<p>The post <a href="https://significantfigurescalculator.com/tools-code/python/rounding-to-significant-figures-python/">Rounding to Significant Figures in Python: Precision, Pitfalls, and Best Practices</a> appeared first on <a href="https://significantfigurescalculator.com">SignificantFiguresCalculator</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://significantfigurescalculator.com/tools-code/python/rounding-to-significant-figures-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sig Figs in R, MATLAB, JavaScript, and SQL — Behaviour Compared</title>
		<link>https://significantfigurescalculator.com/tools-code/sig-figs-in-r-matlab-javascript-and-sql-behaviour-compared/</link>
					<comments>https://significantfigurescalculator.com/tools-code/sig-figs-in-r-matlab-javascript-and-sql-behaviour-compared/#respond</comments>
		
		<dc:creator><![CDATA[Tommy C. Moran]]></dc:creator>
		<pubDate>Thu, 02 Jul 2026 17:01:25 +0000</pubDate>
				<category><![CDATA[Tools & code]]></category>
		<guid isPermaLink="false">http://significantfigurescalculator.test/uncategorized/sig-figs-in-r-matlab-javascript-and-sql-behaviour-compared/</guid>

					<description><![CDATA[<p>Rule Statement Significant figures (sig figs) are the digits in a number that carry meaning contributing to its measurement resolution. The rules for determining sig figs are well established: all non-zero digits are significant; zeros between non-zero digits are significant; leading zeros are not significant; trailing zeros are significant only if the number contains a [&#8230;]</p>
<p>The post <a href="https://significantfigurescalculator.com/tools-code/sig-figs-in-r-matlab-javascript-and-sql-behaviour-compared/">Sig Figs in R, MATLAB, JavaScript, and SQL — Behaviour Compared</a> appeared first on <a href="https://significantfigurescalculator.com">SignificantFiguresCalculator</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2 id="rule-statement">Rule Statement</h2>
<p>Significant figures (sig figs) are the digits in a number that carry meaning contributing to its measurement resolution. The rules for determining sig figs are well established: all non-zero digits are significant; zeros between non-zero digits are significant; leading zeros are not significant; trailing zeros are significant only if the number contains a decimal point. When rounding to a given number of sig figs, the standard convention (as per <a href="/standards/astm-e29">ASTM E29</a> and <a href="/standards/iso-80000">ISO 80000</a>) is to round half away from zero, unless otherwise specified. However, programming languages and database systems often implement different rounding modes, leading to discrepancies that can affect scientific and engineering calculations.</p>
<p>This article compares the default rounding behaviour of R, MATLAB, JavaScript, and SQL, highlighting where they align with or deviate from the standards. It also provides practical guidance for achieving consistent sig-fig handling across these environments.</p>
<h2 id="software-behavior-note">Software Behavior Note</h2>
<h3 id="r">R</h3>
<p>R&#8217;s <code>round()</code> function uses <em>banker&#8217;s rounding</em> (round half to even) by default. For example, <code>round(2.5)</code> returns 2, and <code>round(3.5)</code> returns 4. This is consistent with the IEEE 754 standard for floating-point arithmetic, but it differs from the common “round half up” taught in many classrooms. To round half away from zero, use <code>round(x, digits = 0, ...)</code> with the <code>round5</code> argument in newer versions, or use <code>zapsmall()</code> for display purposes. For sig figs, R&#8217;s <code>signif()</code> function rounds to a specified number of significant digits, but it also uses banker&#8217;s rounding for ties.</p>
<h3 id="matlab">MATLAB</h3>
<p>MATLAB&#8217;s <code>round()</code> function rounds half away from zero. For example, <code>round(2.5)</code> returns 3, and <code>round(-2.5)</code> returns -3. This matches the ASTM E29 default. MATLAB also provides <code>round(X, N)</code> for rounding to N decimal places, and <code>round(X, N, 'significant')</code> for rounding to N significant digits. The latter uses the same half-away-from-zero rule.</p>
<h3 id="javascript">JavaScript</h3>
<p>JavaScript&#8217;s <code>Math.round()</code> rounds half up (towards positive infinity) for positive numbers, but for negative numbers it rounds half towards positive infinity as well (e.g., <code>Math.round(-2.5)</code> returns -2, not -3). This is asymmetric and can introduce bias in data processing. For sig figs, JavaScript lacks a built-in <code>signif()</code> function; developers must implement custom logic using <code>toPrecision()</code> or <code>toExponential()</code>, which use round half up (towards positive infinity) for all numbers.</p>
<h3 id="sql">SQL</h3>
<p>SQL behaviour varies by database system. MySQL&#8217;s <code>ROUND()</code> rounds half away from zero. PostgreSQL&#8217;s <code>ROUND()</code> for numeric types also rounds half away from zero, but for double precision it uses round half to even (banker&#8217;s rounding). SQL Server&#8217;s <code>ROUND()</code> uses round half away from zero by default, but offers a third parameter to control truncation. Oracle&#8217;s <code>ROUND()</code> rounds half up. There is no standard SQL function for significant digits; you must use <code>ROUND(value, -n)</code> with negative decimal places to round to tens, hundreds, etc., but this only works for powers of ten, not arbitrary sig figs.</p>
<h2 id="worked-examples">Worked Examples</h2>
<h3 id="example-1-rounding-0-0004567-to-2-sig-figs">Example 1: Rounding 0.0004567 to 2 sig figs</h3>
<p>In R: <code>signif(0.0004567, 2)</code> returns 0.00046 (since the first two significant digits are 4 and 5, and the next digit 6 rounds up). In MATLAB: <code>round(0.0004567, 2, 'significant')</code> returns 0.00046. In JavaScript: <code>Number(0.0004567.toPrecision(2))</code> returns 0.00046. In SQL (MySQL): <code>ROUND(0.0004567, 2)</code> would round to 2 decimal places, not 2 sig figs; you need <code>ROUND(0.0004567, -2)</code>? Actually that rounds to hundreds. For sig figs, you&#8217;d need to use scientific notation: <code>ROUND(0.0004567 * 1000, 0) / 1000</code>? That&#8217;s messy. The correct approach is to use <code>ROUND(0.0004567, 2 - FLOOR(LOG10(0.0004567)) - 1)</code> but that&#8217;s complex. We&#8217;ll note that SQL lacks a direct sig-fig function.</p>
<h3 id="example-2-rounding-2-5-to-1-sig-fig">Example 2: Rounding 2.5 to 1 sig fig</h3>
<p>R: <code>signif(2.5, 1)</code> returns 2 (banker&#8217;s rounding). MATLAB: <code>round(2.5, 1, 'significant')</code> returns 3 (half away from zero). JavaScript: <code>Number(2.5.toPrecision(1))</code> returns 3 (half up). SQL (MySQL): <code>ROUND(2.5, 0)</code> returns 3 (half away from zero). This example highlights the critical difference in tie handling.</p>
<h2 id="counter-examples">Counter-Examples</h2>
<p><strong>Counter-example 1: Using <code>round()</code> in R for scientific data</strong> — A chemist measures a mass of 2.5 g and needs to report it to 1 sig fig. Using R&#8217;s default <code>round(2.5, 0)</code> yields 2, which under-reports the measurement. The correct value per ASTM E29 is 3. This can lead to significant errors in subsequent calculations.</p>
<p><strong>Counter-example 2: JavaScript&#8217;s asymmetric rounding</strong> — In a financial application, summing values like -2.5 and 2.5 using <code>Math.round()</code> gives -2 and 3, respectively, introducing a bias of +0.5. This violates the principle of unbiased rounding required by many standards.</p>
<p><strong>Counter-example 3: SQL&#8217;s inconsistent behaviour across DBMS</strong> — A data pipeline that moves from MySQL to PostgreSQL may produce different results for the same query, because PostgreSQL&#8217;s double precision uses banker&#8217;s rounding while MySQL uses half away from zero. This can break reproducibility in scientific databases.</p>
<h2 id="convention-comparison-table">Convention Comparison Table</h2>
<table>
<thead>
<tr>
<th>Language/System</th>
<th>Default Rounding Mode</th>
<th>Sig Fig Function</th>
<th>Tie Handling</th>
<th>Standards Alignment</th>
</tr>
</thead>
<tbody>
<tr>
<td>R</td>
<td>Banker&#8217;s rounding</td>
<td><code>signif()</code></td>
<td>Round half to even</td>
<td>IEEE 754, not ASTM E29</td>
</tr>
<tr>
<td>MATLAB</td>
<td>Half away from zero</td>
<td><code>round(...,'significant')</code></td>
<td>Round half away from zero</td>
<td>ASTM E29, ISO 80000</td>
</tr>
<tr>
<td>JavaScript</td>
<td>Half up (towards +∞)</td>
<td><code>toPrecision()</code></td>
<td>Round half up</td>
<td>Not aligned with common standards</td>
</tr>
<tr>
<td>SQL (MySQL)</td>
<td>Half away from zero</td>
<td>None (manual)</td>
<td>Round half away from zero</td>
<td>ASTM E29</td>
</tr>
<tr>
<td>SQL (PostgreSQL)</td>
<td>Numeric: half away; double: banker&#8217;s</td>
<td>None (manual)</td>
<td>Varies by type</td>
<td>Inconsistent</td>
</tr>
</tbody>
</table>
<h2 id="standards-citation">Standards Citation</h2>
<p>ASTM E29-22, “Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications,” specifies that when rounding to a given number of significant digits, the “round half away from zero” rule shall be used unless otherwise stated (Section 6.1.2). ISO 80000-1:2022, “Quantities and units – Part 1: General,” recommends rounding to the nearest value with ties rounded to the nearest even digit (Section 7.4) for statistical purposes, but allows other conventions when clearly stated. NIST Technical Note 1297, “Guidelines for Evaluating and Expressing the Uncertainty of NIST Measurement Results,” advises that rounding of measurement results should be consistent with the uncertainty, and that the number of significant figures should reflect the uncertainty (Section 7.2). The GUM (JCGM 100:2008) similarly emphasizes that reported results should not have more digits than justified by the uncertainty (Clause 7.2.6).</p>
<p>In practice, for engineering and scientific reporting, ASTM E29 is the most commonly cited standard for rounding to significant figures. Therefore, MATLAB&#8217;s default behaviour aligns well, while R and JavaScript require explicit adjustment to meet this standard.</p>
<h2 id="common-mistakes">Common Mistakes</h2>
<ul>
<li><strong>Assuming all languages round half up</strong> — As shown, R uses banker&#8217;s rounding, and JavaScript uses half up for positive but half towards positive infinity for negative numbers.</li>
<li><strong>Using <code>round()</code> for sig figs instead of a dedicated function</strong> — In R, <code>round(x, digits)</code> rounds to decimal places, not significant digits. Use <code>signif()</code>.</li>
<li><strong>Ignoring floating-point representation</strong> — In JavaScript, <code>0.1 + 0.2</code> is not exactly 0.3, so rounding to sig figs can produce unexpected results. Always use <code>toPrecision()</code> on the result.</li>
<li><strong>Applying SQL <code>ROUND()</code> with negative digits for sig figs</strong> — <code>ROUND(1234, -2)</code> rounds to hundreds, not to 2 sig figs. For 2 sig figs, you need to round to the nearest 10, which is <code>ROUND(1234, -1)</code> — but this only works for certain magnitudes.</li>
<li><strong>Not considering the uncertainty</strong> — Sig figs are meaningless without an associated uncertainty. Always report the uncertainty and round the measurement to match its magnitude.</li>
</ul>
<h2 id="quick-reference-table">Quick Reference Table</h2>
<table>
<thead>
<tr>
<th>Task</th>
<th>R</th>
<th>MATLAB</th>
<th>JavaScript</th>
<th>SQL (MySQL)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Round to 3 decimal places</td>
<td><code>round(x, 3)</code></td>
<td><code>round(x, 3)</code></td>
<td><code>Number(x.toFixed(3))</code></td>
<td><code>ROUND(x, 3)</code></td>
</tr>
<tr>
<td>Round to 3 sig figs</td>
<td><code>signif(x, 3)</code></td>
<td><code>round(x, 3, 'significant')</code></td>
<td><code>Number(x.toPrecision(3))</code></td>
<td>Manual: <code>ROUND(x, 2 - FLOOR(LOG10(x)))</code></td>
</tr>
<tr>
<td>Round half away from zero</td>
<td><code>round(x, 0, round5 = TRUE)</code> (R ≥ 4.0)</td>
<td><code>round(x)</code></td>
<td>Custom function</td>
<td><code>ROUND(x, 0)</code></td>
</tr>
<tr>
<td>Round half to even</td>
<td><code>round(x)</code></td>
<td>Not default</td>
<td>Not default</td>
<td>PostgreSQL double: <code>ROUND(x::double precision)</code></td>
</tr>
</tbody>
</table>
<h2 id="sources-further-reading">Sources &amp; Further Reading</h2>
<ul>
<li>ASTM E29-22, “Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications,” ASTM International, 2022.</li>
<li>ISO 80000-1:2022, “Quantities and units – Part 1: General,” ISO, 2022.</li>
<li>NIST Technical Note 1297, “Guidelines for Evaluating and Expressing the Uncertainty of NIST Measurement Results,” 1994.</li>
<li>JCGM 100:2008, “Evaluation of measurement data — Guide to the expression of uncertainty in measurement (GUM),” BIPM, 2008.</li>
<li>R Core Team, “R: A Language and Environment for Statistical Computing,” R Foundation, 2024. <a href="/software/r">R documentation</a></li>
</ul>
<p>For more on rounding methods, see our <a href="/rounding-methods">Rounding Methods</a> guide. For a deeper dive into significant figure rules, visit <a href="/significant-figures-rules">Significant Figures Rules</a>.</p>
<p>The post <a href="https://significantfigurescalculator.com/tools-code/sig-figs-in-r-matlab-javascript-and-sql-behaviour-compared/">Sig Figs in R, MATLAB, JavaScript, and SQL — Behaviour Compared</a> appeared first on <a href="https://significantfigurescalculator.com">SignificantFiguresCalculator</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://significantfigurescalculator.com/tools-code/sig-figs-in-r-matlab-javascript-and-sql-behaviour-compared/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
