from Significant Figures by Teck Por Lim
Workaround for fprintf/sprintf to produce correct output for signficant figures, as fprintf/sprintf

example.m
%[strOut2] = sigfig(matNum, nSigFig, dummy)
%Rounds number to nSigFig number of significant figures and outputs a string
%'pad' in 3rd argument to have padded zeros
%Lim Teck Por, 2006, 2008, 2009
%Apropos: mat2str, num2str, sprintf
%This code is to serve as a workaround to inconsistent rounding in MATLAB and print zero padding:

%num2str(3.15, 2)
%mat2str(3.15, 2)
%fprintf('%1.2g\n', 3.15)
%sprintf('%1.2g\n', 3.15)
%give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8.

%It seems that the fprintf and sprintf problem has also appeared in C and Python. I am not sure about the other languages.

sigfig(3.15, 2) %gives "3.2"
sigfig(0.0375, 2, []) %gives "3.8e-2"

sigfig(-[1.15e2, 1.15, 1.15e-2], 4, 'pad')
sigfig(-[1.15e2, 1.15, 1.15e-2], 4, [])
sigfig(-[1.15e2, 1.15, 1.15e-2], 4)
sigfig(-[1.15e2, 1.15, 1.15e-2], 2, 'pad')
sigfig(-[1.15e2, 1.15, 1.15e-2], 2, [])
sigfig(-[1.15e2, 1.15, 1.15e-2], 2)
sigfig(-[1.15e10, 1.15, 1.15e-10], 2, 'pad')
sigfig(-[1.15e10, 1.15, 1.15e-10], 2, [])
sigfig(-[1.15e10, 1.15, 1.15e-10], 2)

Contact us at files@mathworks.com