EngineeringNotationFormatter

C-based Engineering Notation Formatter with a Objective-C wrapper.


License
BSD-3-Clause
Install
pod try EngineeringNotationFormatter

Documentation

EngineeringNotationFormatter

iOS Project demoing C-based Engineering Notation Formatter with a Objective-C wrapper, and handles variable digits, style, and step capability.

CocoaPod

The easiest way to use EngineeringNotationFormatter in your project is by including the pod:

pod 'EngineeringNotationFormatter'

You can then use the formatting function like this:

#import "DHengNotation.h"

...

DHToEngineeringString(4731230.f, 3, NO); // => 4.73 M

Notes

Martin Moene has created a really nice C++ version, see https://github.com/martinmoene/EngFormat-Cpp

Release notes

v 1.2 - after Martin Moene found some edge cases that produces failures, redid much of the code
      - log messages can be enabled or not in EngNotation.c
      - handle input values which are not normal floats and return Nan, Infinite, etc as appropriate
      - incorporated Unit Tests based on the ones written by Martin Moene (we now share them)
v 1.0 - initial release, basically as I got i from Jukka Korpela

Historical

Several years ago I tripped on a great C function that formats a floating point number into engineering notation, in either exponential or International System of Units (SI) notation. This C code was written by Jukka Korpela and posted at http://www.cs.tut.fi/~jkorpela/c/eng.html.

This function is useful when you want to display say, length, in units people (not machines) can understand. Thus, instead of "1.67e4 meters" you could show either "16.7e3 meters", or using the SI prefixes (http://physics.nist.gov/cuu/Units/prefixes.html) as "17.7 k meters".

As Jukka points out, there is no POSIX (i.e. printf) formatter that will do this, so he wrote one. In using his code, I found an edge condition and provided updated code, which Jukka appended to his web page circa 2009.

The code has a most interesting feature; the ability to specify the number of significant digits. For instance, if you set the value to 4, every number will be rounded up or down so that it is comprises exactly that many digits. Thus, you can utilize this code to provide a floating point step function, that exactly steps a number by one increment (or decrement) to its lowest significant digit. There are convenience functions in both C and Objective-C to perform this step.

A reverse function is also provided that takes the string in either exponential or SI units and returns the floating point number properly rounded.

For sure, this feature is not one many people need, but it you do need it, this code is invaluable, and I'm sure Jukka spent a good deal of time working on it.

The included iOS project can be used to experiment with the various settings:

ScreenShot

We jointly offer this code here with a unattributed BSD style license (see source files).