Explained clearly 5 min read

How to Round to Significant Figures in MATLAB: Precision Control for Engineers

MATLAB has no single sig-fig rounding function, but you can combine round, floor, log10, and sprintf/fprintf to control significant digits accurately.

Short Answer

MATLAB has no single sig-fig rounding function, but you can combine round, floor, log10, and sprintf/fprintf to control significant digits accurately.

Rounding to significant figures is a measurement decision, not merely a formatting trick. MATLAB is a numerical environment: it stores IEEE 754 double-precision values and does not ship with a dedicated sigfig function. This reference explains the precise rules, the MATLAB functions that matter, and the engineering conventions you should follow when reporting results.

Rule Statement

To round a nonzero number x to n significant figures:

  1. Find the order of magnitude: m = floor(log10(abs(x))).
  2. Scale so the first significant digit sits in the units place: scale = 10^(n - 1 - m).
  3. Round the scaled value and divide back: y = round(x * scale) / scale.

In MATLAB, the compact numeric expression is y = round(x, n - 1 - floor(log10(abs(x)))) when x is nonzero. For x = 0, return 0 and handle formatting separately because log10(0) is -Inf. For display rather than numeric storage, sprintf('%.*g', n, x) or num2str(x, n) formats to n significant digits.

See Rounding Rules for the general digit-by-digit procedure and Significant Figures for counting conventions.

Software Behavior Note

MATLAB’s round has two important behaviors. First, round(x, k) rounds to k decimal places, not k significant figures. round(123.456, 3) returns 123.456, while three significant figures would be 123. Second, round breaks ties away from zero: round(0.5) is 1 and round(-0.5) is -1. That is not banker’s rounding; if you need round-half-to-even, implement it explicitly or use an external function. See Banker’s Rounding.

format short, format long, and format short e change only the command-window display. They do not round the stored value. num2str(x, n) and sprintf('%.*g', n, x) produce character output with n significant digits. To preserve trailing zeros as significant, use the # flag: sprintf('%#.*g', n, x) renders 12 as 12.0 for n = 3.

For arrays, vectorize with a zero guard:

scale = 10.^(n - 1 - floor(log10(abs(x)))); scale(x == 0) = 1; y = round(x .* scale) ./ scale; y(x == 0) = 0;

Worked Examples

Example 1: 123.456 to 3 significant figures

m = floor(log10(123.456)) = 2; exponent = 3 - 1 - 2 = 0; round(123.456, 0) = 123. Result: 123.

Example 2: 0.00123456 to 3 significant figures

m = floor(log10(0.00123456)) = -3; exponent = 3 - 1 - (-3) = 5; round(0.00123456, 5) = 0.00123. Result: 0.00123.

Example 3: 9.876 to 3 significant figures

m = 0; exponent = 2; round(9.876, 2) = 9.88. Note that this is not the same as rounding to 2 decimal places in all cases; the rule tracks the first significant digit.

Example 4: -0.04567 to 2 significant figures

abs(x) = 0.04567; m = -2; exponent = 2 - 1 - (-2) = 3; round(-0.04567, 3) = -0.046. Result: -0.046. The sign is retained; the significant digits are 4 and 6.

Counter-Examples

  • Decimal places mistaken for significant figures: round(9.876, 3) gives 9.876, not 9.88.
  • Display used as rounding: format short may show 3.1416, but pi remains full precision in memory. Do not rely on display for compliance data.
  • Double rounding: rounding 0.4445 to 3 significant figures via an intermediate 2-decimal step can produce 0.445 instead of the correct 0.444 under some tie rules. Round once, at the end. See Double Rounding Error.
  • Ignoring zero: floor(log10(0)) returns -Inf; the formula fails unless zero is explicitly handled.
  • Forgetting abs: negative inputs produce complex logarithms or errors. Always use log10(abs(x)).

Quick Reference Table

Task MATLAB expression Notes
Numeric rounding to n sig figs round(x, n - 1 - floor(log10(abs(x)))) Guard x == 0.
Display n sig figs sprintf('%.*g', n, x) Returns a string.
Preserve trailing zeros sprintf('%#.*g', n, x) Example: 12 becomes 12.0 for n = 3.
Truncate to n sig figs fix(x .* 10.^(n - 1 - floor(log10(abs(x))))) ./ 10.^(n - 1 - floor(log10(abs(x)))) Toward zero; use floor or ceil for directed truncation.
Check tie behavior round(0.5), round(-0.5) Returns 1 and -1; MATLAB ties away from zero.

For notation-specific rules, see Scientific Notation and Engineering Notation.

Standards Citation

Precision practice is governed by consensus standards. ASTM E29-22 covers the use of significant digits in test data and conformance decisions. ISO 80000-1:2009, Clause 6.4 gives general rounding rules for quantities and units. NIST SP 811, Section 8.1 provides SI rounding guidance, and JCGM 100:2008 (GUM), Clause 7.2.6 recommends reporting uncertainty to one or two significant digits. Use these when a project, lab, or regulator requires traceable rounding. See Measurement Uncertainty and GUM Guide.

Discipline Note

Engineering and physical-science reporting usually follows three principles. First, keep full precision during calculation; round only the final reported value. Second, let measurement uncertainty set the last significant digit. Third, match the reported value to the uncertainty: round uncertainty to one or two significant digits, then round the estimate to the same decimal place. In chemistry, addition/subtraction uses decimal-place rules while multiplication/division uses significant-figure rules; MATLAB does not infer those measurement rules automatically. In astronomy and physics, scientific notation avoids ambiguous trailing zeros. See Standard Deviation Reporting.

Common Mistakes

  • Using round(x, n) as if it rounded to n significant figures.
  • Confusing stored values with formatted strings.
  • Rounding intermediate MATLAB variables and propagating the error.
  • Assuming MATLAB uses banker’s rounding; it does not by default.
  • Dropping significant trailing zeros when the number is displayed or exported.
  • Using the sig-fig formula on zero without a guard.

MATLAB precision is not the same as measurement precision. A double can hold 15–17 significant decimal digits, but a reported result should carry only the digits justified by the measurement.

Sources & Further Reading

  • MathWorks. round, sprintf, and num2str documentation. mathworks.com/help/matlab/ref/round.html
  • ASTM E29-22. Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications. ASTM International.
  • ISO 80000-1:2009. Quantities and units — Part 1: General, Clause 6.4.
  • NIST SP 811. Guide for the Use of the International System of Units (SI), Section 8.1.
  • JCGM 100:2008. Evaluation of measurement data — Guide to the expression of uncertainty in measurement (GUM), Clause 7.2.6.

FAQ

Does MATLAB have a built-in significant figures function?

No single function rounds to significant figures. Use round(x, n - 1 - floor(log10(abs(x)))) for numeric rounding, or sprintf('%.*g', n, x) and num2str(x, n) for formatted output.

How do I round to 3 significant figures in MATLAB?

For nonzero x, use round(x, 3 - 1 - floor(log10(abs(x)))). Handle x == 0 separately. For display, use sprintf('%.3g', x).

Does MATLAB round 0.5 up or to even?

MATLAB round rounds ties away from zero: round(0.5) is 1 and round(-0.5) is -1. For round-half-to-even, implement a custom rule or use a toolbox function.

How do I keep trailing zeros significant in MATLAB?

Use the # flag: sprintf('%#.*g', n, x). For example, sprintf('%#.3g', 12) returns 12.0.

Verified sources

References

  1. MathWorks. round, sprintf, and num2str documentation. https://www.mathworks.com/help/matlab/ref/round.html
  2. ASTM E29-22. Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications. ASTM International.
  3. ISO 80000-1:2009. Quantities and units — Part 1: General, Clause 6.4.
  4. NIST SP 811. Guide for the Use of the International System of Units (SI), Section 8.1.
  5. JCGM 100:2008. Evaluation of measurement data — Guide to the expression of uncertainty in measurement (GUM), Clause 7.2.6.

Leave a Reply

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