File Exchange

image thumbnail

num2eng

version 1.1.3 (4.43 KB) by HarryDymond
Convert numbers to engineering notation strings, or optionally using SI-prefixes instead

8 Downloads

Updated 16 Jul 2019

View License

Converts an input numerical value into an engineering-formatted string, in either scientific format with multiples-of-three exponent, or using SI prefixes e.g. k, M, n, p etc. Also supports vector/matrix inputs.

Firstly, let me address the question: Do we really need another one of these? There are already a few num2eng and num2si functions on the file exchange. What makes this one unique is:
It supports scalar, vector, and matrix inputs
It returns strings using either SI prefixes or engineering-formatted scientific notation
It can optionally use the Greek lower-case mu (unicode U+03BC) as the SI prefix for numbers with magnitude >=1e-6 and <1e-9.
It can optionally use the true minus character (unicode U+2212) instead of hyphen-minus (U+002D)
It properly handles edge-cases such as rounding, empty inputs, non-numerical inputs etc.
It supports complex number inputs.

Syntax: string = num2eng(number,[useSIPrefix],[useMu],[spaceAfterNumber],[sigFigs],[fullName],[useMinus]), where input variables in square brackets are optional - i.e. The function accepts anywhere from 1 to 7 inputs, inclusive.

Input variables:
number: The numeric value(s) to be converted to a string. If this is the only input, the string returned will be in scientific format with multiples-of-three exponent. e.g. num2eng(0.001) will return the string '1e-3' (not including quotes). If number is empty (i.e. []) or not numeric, the returned string will be '', or ' ' if spaceAfterNumber=true. If "number" is a vector or matrix, num2eng will return a cell matrix with the same "shape" as the number matrix, with one string per cell. Complex numbers are supported, but if the imaginary part is non-zero, SI prefixes will not be used.

useSIPrefix: Logical. (Or can use 0/1 instead of true/false). When set to true, SI prefixes will be used for any abs(number) in the range 1 y to 999.999... Y, instead of scientific notation.

useMu: Logical. If useSIPrefix=true, and abs(number) is >=1e-6 and <1e-9, the lower-case greek mu (unicode U+03BC) will be used. Otherwise, 'u' will be used here instead.

spaceAfterNumber: Logical. When set to true, a space will be inserted after the numeric part of the output string, after the 'e' if the output string is in scientific format, and in all cases where useSIPrefix=true, even for numbers between -999.99... and +999.99...

sigFigs: Integer. Specifies how many significant figures should be in the output string. Defaults to 5 if not specified.

fullName: Logical. If fullName=true, the full name of the SI prefix will be used, rather than just a letter.

useMinus: Logical. If useMinus=true, and the input number and/or the exponent is negative, the returned string will use the "proper" minus character (U+2212) instead of the default ascii hyphen-minus (U+002D).

Examples ('x' means 'don't care'/'not relevant' in the below examples):
num2eng(0.001,true) will return the string '1 m'
num2eng(2.56e29,true) will return the string '256e+27'
num2eng(1.5,true) will return the string '1.5'
num2eng(-1.2e5,1) will return the string '-120 k'

num2eng(10e-6,true) will return the string '10 u'
num2eng(10e-6,true,true) will return the string '10 <mu>', where <mu> is the lower-case Greek mu character (unicode U+03BC)

num2eng(0.001,true,x,x) will return the string '1 m'
num2eng(0.001,false,x,true) will return the string '1e-3 '
num2eng(2.56e29,true,x,true) will return the string '256e+27 '
num2eng(1.5,true,x,true) will return the string '1.5 '

num2eng(0.001,1,0,0,5,1) will return the string '1 milli'
num2eng(2.56e29,true,false,false,5,true) will return the string '256e+27'
num2eng(1.5,true,false,false,5,true) will return the string '1.5'
num2eng(999999,true,0,0,4,true) will return the string '1 mega'

num2eng(-0.001,false,x,x,x,false,true) will return the string '−1e−3'

Cite As

Harry Dymond (2019). num2eng (https://www.mathworks.com/matlabcentral/fileexchange/67740), MATLAB Central File Exchange. Retrieved July 16, 2019.

Comments and Ratings (4)

Great, now it works. Thanks a lot.

HarryDymond

Hello again @Chang hsiung. I believe the problem was with use of the iptnum2ordinal function. Somehow, my copy of MATLAB managed to execute the iptnum2ordinal function without checking out a license to the image processing toolbox! Anyway, I have now replaced calls to that function so you should be able to use num2eng now.

HarryDymond

@Chang hsiung, thank you for your interest in my function. What version of MATLAB are you using? The function was developed in MATLAB version 2016a, and according to my testing, should not require the Image Processing Toolbox. If you are using at least version 2016a of MATLAB, please could you let me know the exact num2eng call you are making?

Do you have a version that does not require Image Processing toolbox ???

Updates

1.1.3

If input number is minus infinity, the useMinus option is now respected

1.1.2

Minor code cleanup (no changes in functionality)

1.1.1

Changed icon

1.1

Added the option to use the true minus character (U+2212) in returned strings. The new option is added in such a way that the function will remain backwards compatible - code using earlier versions of num2eng does not need to be changed.

1.0.6

function input no longer uses varargin, to make function hints more useful. Inputs 2 to 6 remain optional.

1.0.4.0

Function should no longer require the Image Processing Toolbox

1.0.3.0

Option "fullName" now overrides option "useMu"
If input is a column vector, output will now be a column vector of cells instead of a row vector of cells

1.0.2.0

Updated description

1.0.1.0

Updated description

1.0.1.0

Updated description

1.0.1.0

Updated description

1.0.1.0

updated FEX icon

1.0.1.0

changed FEX icon

1.0.1.0

changed FEX icon

1.0.1.0

fixed a typo

1.0.1.0

fixed a typo

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: Number to Scientific Prefix