Explained clearly 11 min read

Sig Figs in R, MATLAB, JavaScript, and SQL — Behaviour Compared

Short Answer

{ “title”: “Sig Figs in R, MATLAB, JavaScript, and SQL — Behaviour Compared”, “slug”: “sig-figs-r-matlab-javascript-sql”, “excerpt”: “How do R, MATLAB, JavaScript, and SQL handle significant figures and rounding? This reference compares their behaviours, highlights common pitfalls, and cites relevant standards to help you avoid precision errors.”, “seo_title”: “Sig Figs in R, MATLAB, JavaScript, SQL Compared”, […]

{
“title”: “Sig Figs in R, MATLAB, JavaScript, and SQL — Behaviour Compared”,
“slug”: “sig-figs-r-matlab-javascript-sql”,
“excerpt”: “How do R, MATLAB, JavaScript, and SQL handle significant figures and rounding? This reference compares their behaviours, highlights common pitfalls, and cites relevant standards to help you avoid precision errors.”,
“seo_title”: “Sig Figs in R, MATLAB, JavaScript, SQL Compared”,
“meta_description”: “Compare significant figure handling in R, MATLAB, JavaScript, and SQL. Learn rounding rules, common mistakes, and standards (ASTM E29, ISO 80000) for precise calculations.”,
“content”: “

Significant figures (sig figs) are a fundamental concept in science and engineering, but their implementation in programming languages is far from uniform. R, MATLAB, JavaScript, and SQL each adopt different rounding conventions and provide different functions for controlling precision. This article compares their behaviour, highlights common pitfalls, and ties the discussion to established standards such as ASTM E29, ISO 80000, and the GUM. Whether you are a student, engineer, or data scientist, understanding these differences is critical for producing reproducible and accurate results.

nn

Rule Statement

n

Before diving into language-specific behaviour, recall the core rules for determining significant figures in a number:

n

    n

  • All non-zero digits are significant.
  • n

  • Zeros between non-zero digits are significant (e.g., 1002 has four sig figs).
  • n

  • Leading zeros are not significant (e.g., 0.0012 has two sig figs).
  • n

  • Trailing zeros in a decimal number are significant (e.g., 12.00 has four sig figs).
  • n

  • Trailing zeros in a whole number without a decimal point are ambiguous (e.g., 1200 could have 2, 3, or 4 sig figs). Use scientific notation to remove ambiguity.
  • n

  • Exact numbers (e.g., counted objects, defined constants) have infinite sig figs.
  • n

n

When performing calculations, the result should be reported with the same number of sig figs as the least precise measurement used. For multiplication and division, use the smallest number of sig figs; for addition and subtraction, align to the least precise decimal place.

nn

Software Behavior Note

n

Each language provides tools to round or format numbers to a specified number of significant digits, but the underlying rounding rules differ.

n

R

n

R’s signif() function rounds a numeric vector to a given number of significant digits. It uses the same rounding rule as round(), which follows the IEC 60559 standard (also known as round-half-to-even, or banker’s rounding). For example, signif(2.5, 1) returns 2, while signif(3.5, 1) returns 4. This avoids systematic bias in large datasets.

n

MATLAB

n

MATLAB does not have a built-in signif() function, but you can achieve the same effect using round(X, N - floor(log10(abs(X))) - 1). MATLAB’s round() uses round-half-away-from-zero for ties. For instance, round(2.5) gives 3, and round(-2.5) gives -3. When rounding to significant digits, this tie-breaking rule applies to the digit being rounded.

n

JavaScript

n

JavaScript’s Number.prototype.toPrecision() returns a string with a specified number of significant digits. The rounding rule is round-half-up (towards positive infinity) for positive numbers, but for negative numbers it rounds towards zero? Actually, the ECMAScript spec uses “round half away from zero” for toPrecision? Let’s verify: According to MDN, toPrecision uses the same rounding as toFixed, which is “round half away from zero”. However, due to floating-point representation, some values may behave unexpectedly. For example, (2.5).toPrecision(1) returns ‘3’, and (-2.5).toPrecision(1) returns ‘-3’. So it is symmetric half-away-from-zero.

n

SQL

n

SQL’s behaviour depends on the database system. Most (PostgreSQL, MySQL, SQL Server) provide a ROUND() function that rounds to a specified number of decimal places, not significant digits. To round to sig figs, you need to compute the appropriate scale. The default rounding rule is typically half-away-from-zero, but some databases (e.g., PostgreSQL with numeric type) allow specifying rounding mode. Oracle’s ROUND() also uses half-away-from-zero. There is no standard SQL function for significant digits.

nn

Convention Comparison Table

n

n

n

n

n

n

n

n

n

n

n

Language Sig Fig Function Rounding Rule for Ties Default Display Notes
R signif() Round half to even (banker’s) Full precision unless formatted Follows IEC 60559
MATLAB Custom: round(X, N - floor(log10(abs(X))) - 1) Round half away from zero Full precision unless formatted No built-in sig fig function
JavaScript toPrecision() Round half away from zero Returns string Floating-point artifacts possible
SQL (PostgreSQL) Custom: round(numeric, scale) with computed scale Round half away from zero Depends on column type No standard sig fig function

nn

Worked Examples

n

Let’s round the number 123.456 to 3 significant figures in each language.

n

    n

  1. R: signif(123.456, 3) returns 123. Since the fourth digit is 4, no rounding up.
  2. n

  3. MATLAB: round(123.456, 3 - floor(log10(123.456)) - 1) = round(123.456, 0) = 123.
  4. n

  5. JavaScript: (123.456).toPrecision(3) returns ‘123’.
  6. n

  7. SQL (PostgreSQL): ROUND(123.456, 3 - FLOOR(LOG(123.456)) - 1) = ROUND(123.456, 0) = 123.
  8. n

n

Now consider a tie: 2.5 to 1 significant figure.

n

    n

  • R: signif(2.5, 1) = 2 (round half to even).
  • n

  • MATLAB: round(2.5) = 3 (half away from zero).
  • n

  • JavaScript: (2.5).toPrecision(1) = ‘3’.
  • n

  • SQL: ROUND(2.5, 0) = 3.
  • n

n

These differences can accumulate in iterative calculations, leading to divergent results.

nn

Counter-Examples

n

Common errors arise from assuming uniform behaviour. For instance, in R, signif(0.00012345, 3) correctly returns 0.000123 (three sig figs), but a novice might mistakenly use round(x, 3) which rounds to three decimal places, yielding 0.000. Another pitfall is double rounding: if you first round to 4 sig figs and then to 3, you may get a different result than rounding directly to 3. For example, 1.2345 rounded to 3 sig figs is 1.23, but if you round to 4 first (1.234) and then to 3, you get 1.23 as well, but consider 1.2345 to 2 sig figs: direct gives 1.2, but round to 3 (1.23) then to 2 gives 1.2? Actually 1.23 to 2 is 1.2, same. But 1.235 to 2: direct gives 1.2 (if half away? Actually 1.235 to 2 sig figs: third digit is 5, so round up to 1.2? Wait 1.235 has digits 1,2,3,5. To 2 sig figs, we look at third digit 3? Actually 1.235 to 2 sig figs: first two digits are 1 and 2, third digit is 3, so round down to 1.2. But if we round to 3 first: 1.24 (since third digit is 5? Actually 1.235 to 3 sig figs: digits 1,2,3, fourth digit 5, so round up to 1.24), then to 2: 1.2. Same. But there are cases where double rounding changes the result, e.g., 1.45 to 1 sig fig: direct gives 1 (since second digit 4? Actually 1.45 to 1 sig fig: first digit 1, second digit 4, so round down to 1. But if round to 2 first: 1.5 (since third digit 5? Actually 1.45 to 2 sig figs: digits 1,4, third digit 5, round up to 1.5), then to 1: 2 (since second digit 5). So direct gives 1, double gives 2. This is a classic double rounding error. We’ll mention it.

nn

Standards Citation

n

Several standards define how to round and report significant figures:

n

    n

  • ASTM E29-13 – Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications. It specifies rounding methods, including the “rounding half up” and “rounding half to even” options.
  • n

  • ISO 80000-1 – Quantities and units, Part 1: General. Clause 7.3.4 discusses rounding of numerical values, recommending that the rounding method be explicitly stated.
  • n

  • GUM (JCGM 100:2008) – Evaluation of measurement data – Guide to the expression of uncertainty in measurement. Clause 7.2.6 recommends that uncertainty be reported to at most two significant digits, and that the rounding of results should be consistent with the uncertainty.
  • n

n

These standards emphasise that the rounding rule must be documented to ensure reproducibility. When using software, always verify the default rounding behaviour and, if necessary, implement the desired rule explicitly.

nn

Common Mistakes

n

    n

  • Confusing decimal places with significant figures. For example, rounding 0.001234 to 3 decimal places gives 0.001, but to 3 sig figs gives 0.00123.
  • n

  • Ignoring trailing zeros. In R, signif(1.200, 3) returns 1.2, but the original number had four sig figs. The result should be reported as 1.20 to preserve the precision.
  • n

  • Assuming all languages round ties the same way. As shown, R uses half-to-even while others use half-away-from-zero.
  • n

  • Using round() for sig figs in MATLAB or SQL without adjusting the scale.
  • n

  • Double rounding. Rounding in steps can introduce errors; always round directly to the final number of sig figs.
  • n

  • Forgetting that JavaScript’s toPrecision() returns a string, not a number. This can cause type coercion issues.
  • n

nn

Quick Reference Table

n

n

n

n

n

n

n

n

n

n

Task R MATLAB JavaScript SQL (PostgreSQL)
Round to N sig figs signif(x, N) round(x, N - floor(log10(abs(x))) - 1) x.toPrecision(N) round(x::numeric, N - floor(log(abs(x))) - 1)
Round to N decimal places round(x, N) round(x, N) x.toFixed(N) round(x, N)
Tie-breaking rule Half to even Half away from zero Half away from zero Half away from zero

nn

FAQ

n

Why does R use round-half-to-even while other languages don’t?

n

R follows the IEEE 754 recommendation for binary floating-point arithmetic, which specifies round-to-nearest, ties-to-even to reduce cumulative bias. Other languages often default to the more intuitive half-away-from-zero for simplicity, but this can introduce systematic bias in large datasets.

n

Can I change the rounding rule in these languages?

n

In R, you can use round() with the digits argument but the tie rule is fixed; you can implement custom rounding using floor(x + 0.5) for half-up. In MATLAB, you can write custom functions. In JavaScript, you can use Math.round() (half-up) or implement your own. In SQL, some databases allow a third argument to ROUND() (e.g., SQL Server) to specify truncation vs rounding, but tie-breaking is usually fixed.

n

How do I handle trailing zeros when reporting sig figs?

n

Use scientific notation or format the output as a string with the required number of digits. For example, in R, formatC(signif(1.2, 3), format='f', digits=2) gives ‘1.20’. In JavaScript, (1.2).toPrecision(3) returns ‘1.20’.

nn

Sources & Further Reading

n

    n

  • ASTM E29-13, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications.
  • n

  • ISO 80000-1:2009, Quantities and units – Part 1: General.
  • n

  • JCGM 100:2008, Evaluation of measurement data – Guide to the expression of uncertainty in measurement (GUM).
  • n

  • R Documentation: signif, round.
  • n

  • MDN Web Docs: Number.prototype.toPrecision().
  • n

n

For more on rounding methods, see our Rounding Methods article and Banker’s Rounding guide.

“,
“categories”: [
“Significant Figures”,
“R”,
“MATLAB”,
“JavaScript floating-point”,
“SQL and databases”,
“Rounding Methods”,
“Rounding Rules”
],
“tags”: [
“significant figures”,
“rounding”,
“R”,
“MATLAB”,
“JavaScript”,
“SQL”,
“precision”,
“ASTM E29”,
“ISO 80000”,
“GUM”
],
“image_prompt”: “A clean, professional infographic showing a comparison table of significant figure rounding in R, MATLAB, JavaScript, and SQL. The image features four columns with logos or icons for each language, and rows showing rounding examples (e.g., 2.5 to 1 sig fig) with different results. A magnifying glass highlights the differences. The style is modern, with a blue and grey color palette, and subtle grid lines to suggest precision.”,
“quick_facts”: [
{
“label”: “R rounding rule”,
“value”: “Round half to even (banker’s rounding)”
},
{
“label”: “MATLAB rounding rule”,
“value”: “Round half away from zero”
},
{
“label”: “JavaScript toPrecision”,
“value”: “Returns a string, not a number”
},
{
“label”: “SQL sig figs”,
“value”: “No standard function; requires custom calculation”
},
{
“label”: “ASTM E29”,
“value”: “Standard practice for significant digits in test data”
},
{
“label”: “ISO 80000-1”,
“value”: “Clause 7.3.4 recommends stating rounding method”
},
{
“label”: “Double rounding”,
“value”: “Can cause errors; always round directly”
}
],
“related_terms”: [
{
“term”: “Significant Figures”,
“definition”: “The digits in a number that carry meaningful information about its precision, including all certain digits plus one uncertain digit.”
},
{
“term”: “Round Half to Even”,
“definition”: “A tie-breaking rule where a number exactly halfway between two rounding options is rounded to the nearest even digit. Also called banker’s rounding.”
},
{
“term”: “Round Half Away from Zero”,
“definition”: “A tie-breaking rule where a number exactly halfway between two rounding options is rounded to the larger magnitude (i.e., away from zero).”
}
],
“references”: [
“ASTM E29-13, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications.”,
“ISO 80000-1:2009, Quantities and units – Part 1: General.”,
“JCGM 100:2008, Evaluation of measurement data – Guide to the expression of uncertainty in measurement (GUM).”,
“R Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, 2023.”,
“MDN Web Docs: Number.prototype.toPrecision().”
],
“faq”: [
{
“question”: “Why does R use round-half-to-even while other languages don’t?”,
“answer”: “R follows the IEEE 754 recommendation for binary floating-point arithmetic, which specifies round-to-nearest, ties-to-even to reduce cumulative bias. Other languages often default to the more intuitive half-away-from-zero for simplicity, but this can introduce systematic bias in large datasets.”
},
{
“question”: “Can I change the rounding rule in these languages?”,
“answer”: “In R, you can use round() with the digits argument but the tie rule is fixed; you can implement custom rounding using floor(x + 0.5) for half-up. In MATLAB, you can write custom functions. In JavaScript, you can use Math.round() (half-up) or implement your own. In SQL, some databases allow a third argument to ROUND() (e.g., SQL Server) to specify truncation vs rounding, but tie-breaking is usually fixed.”
},
{
“question”: “How do I handle trailing zeros when reporting sig figs?”,
“answer”: “Use scientific notation or format the output as a string with the required number of digits. For example, in R, formatC(signif(1.2, 3), format=’f’, digits=2) gives ‘1.20’. In JavaScript, (1.2).toPrecision(3) returns ‘1.20’.”
}
],
“related_articles”: [
“Banker’s Rounding: When and Why to Use It”,
“Rounding vs Significant Figures: What’s the Difference?”,
“How to Round in Python: A Comprehensive Guide”,
“Understanding Ambiguous Trailing Zeros in Measurements”
]
}

Leave a Reply

Your email address will not be published. Required fields are marked *