Converting a String to a Double (with SI prefixes)

Engineers and scientists will often use SI prefixes to make writing down very large, and very small, numbers easier. Writing down 3 GV is much better than 3000000000 V :). I’m currently working on a couple of software projects, both at home and work, where I’d like the ability to enter numbers with SI prefixes for convenience.

First I decided to write down the different styles of input my code will have to support, below is the list of styles I came up with.

  • +2.2, or -2.2 => I want to be able to specify the sign of a number explicitly
  • .33333 => I’d like to omit the starting zero
  • 2.2k => Have SI prefixes
  • 2k2 => This style is commonly found in the electronics industry.

Now I know what I want the code to do I can start writing it. I created a dictionary containing the prefixe character and the associated multiplier. I am writing this code in C# by the way, and used LINQPad to try it all out. Once I had it working I put into the class library I was working on.

private readonly Dictionary Prefixes = new Dictionary(){
    {'P', 1e15},
    {'T', 1e12},
    {'G', 1e9},
    {'M', 1e6},
    {'k', 1e3},
    {'h', 1e2},
    {'d', 1e-1},
    {'c', 1e-2},
    {'m', 1e-3},
    {'u', 1e-6},
    {'n', 1e-9},
    {'p', 1e-12},
    {'f', 1e-15},
};

Then I wrote a method that will take in the input string and convert it to a double. The first thing the method does is check to see if it is a plain number that Double.Parse() can take care of, it does this check using a regex. If the Regex matched then it simply calls the Double.Parse() method and returns the result.

If the regex fails then it check using two more regexes if the number looks like it contains SI prefixes. If it does then we find out what prefix is used then remove the prefix and convert the number.

I am not very good with regular expressions so there may be better ways of writing them than this. I have tested this code quite a bit with different types of input and it seems pretty solid. It will throw a FormatException if anything goes wrong.

// These are the regexes used by the method. These are initialised in a constructor.
Regex plain_number_regex = new Regex(@"^[+-]?(?=[\.\d])\d*(\.\d+)?$"); // For .2222, 0.222, 2.32
Regex si_number_a_regex = new Regex(@"^[+-]?[\d]+[PTGMkcmunpf]?[\d]*$"); // for 2k, 2k2
Regex si_number_b_regex = new Regex(@"^[+-]?[\d]+(\.\d+)?[PTGMkcmunpf]?$"); // For 1.2k
 
public double ParseInputStringSI(string input)
{
    // Test to see if it is a plain number with no SI prefixes
    if (plain_number_regex.IsMatch(input)) {
        return Double.Parse(input);
    }
 
    // Test to see if it is a number with an SI prefix.
    if(si_number_a_regex.IsMatch(input) || si_number_b_regex.IsMatch(input) ) {
        // Find where in the string the prefix is and what
        // kind of prefix it is.
        var input_prefix = from p in Prefixes.Keys
                           where input.IndexOf(p) > 0
                           select input[input.IndexOf(p)];
 
        // Make sure the above query worked. There should be
        // no reason for it to fail because the Regex checks
        // the prefix characters.
        if (input_prefix.Count() == 0) {
            throw new FormatException("Invalid Input");
        }
 
        // Get the multiplier for the prefix
        var multiplier = Prefixes[input_prefix.First()];
 
        // Ether replace the prefix with a decimal point or
    // remove it entierly. Depends on the format of the
    // input.
    string inputp;
    if( si_number_a_regex.IsMatch(input) ) {
            inputp = Regex.Replace(input, @"[PTGMkhdcmunpf]", ".");
    } else {
        inputp = Regex.Replace(input, @"[PTGMkhdcmunpf]", "");
    }
 
        // Attempt the conversion, multiply it then return it.
        var tmp = Double.Parse(inputp);
        return tmp * multiplier;
    } else {
        throw new FormatException("Input String is Invalid");
    }
}
     

Getting a List of the Available COM Ports in C#

It’s really nice to show the user a list of the COM ports they actually have on their machines. All too often I have seen software that makes you type in the COM port name. Even worse are the applications that force you to select from a list of COM ports, usually COM1 to COM5, without the option of typing in a different one!

Below is some really simple code that generates a list of the available COM ports and inserts the list into a drop-down selection control in a WinForms application.

string[] ports = SerialPort.GetPortNames();
if (ports.Length > 0) {
    Array.Sort(ports);
    COMPort.Items.AddRange(ports);
    COMPort.Text = ports[0];
} else {
    COMPort.Text = "Unable to Detect COM ports";
}
     

Formatting Numbers using SI Prefixes

Well it’s time for another post. A couple of weeks ago I was working on an interface program for a rig at work. We were adding some extra features to a controller board and I thought while I was working on that I might as well make some changes to the interface program as well.  The feature I wanted to add to the program was to make it format some of the numbers that are displayed in the UI using SI prefixes and with units of measure.

So after a quick look on the web to see if I can find some example code on how to do this, I decided to have a go writing the code on my own without looking at other stuff too much. This is what I came up with, It’s a function written in C# that can be used to convert floating point numbers to formatted strings. So the number 0.102 will be printed as “102m”, and it can append units to the end like this – “102mA”.

public static string FormatStringEng(double input, string units, string format)
{
    string prefix = "";
    double value = 0.0;

    if (input >= 1e+12 && input < 1e+15) {
        prefix = "T"; value = (input / 1e+12);    // Tera (1e+12)
    } else if (input >= 1e+9 && input < 1e+12) {
        prefix = "G"; value = (input / 1e+9);     // Giga (1e+9
    } else if (input >= 1e+6 && input < 1e+9) {
        prefix = "M"; value = (input / 1e+6);     // Mega (1e+6)
    } else if (input >= 1e+3 && input < 1e+6) {
        prefix = "k"; value = (input / 1e+3);     // Kilo (1e+3)
    } else if ((input >= 1) && (input < 1e+6)) {
        prefix = ""; value = input;               // Unity
    } else if (input >= 1e-3 && input < 1) {
        prefix = "m"; value = (input * 1e+3);     // Milli (1e-3)
    } else if (input >= 1e-6 && input < 1e-3) {
        prefix = "μ"; value = (input * 1e+6);     // Micro (1e-6)
    } else if (input >= 1e-9 && input < 1e-6) {
        prefix = "n"; value = (input * 1e+9);     // Nano (1e-9)
    } else if (input >= 1e-12 && input < 1e-9) {
        prefix = "p"; value = (input * 1e+12);    // Pico (1e-12)
    } else if (input >= 1e-15 && input < 1e-12) {
        prefix = "f"; value = (input * 1e+15);    // Fempto (1e-15)
    }

    return value.ToString() + prefix + units;
}

This code is working quite well so far. Haven’t had any problems with it… yet anyway. That’s it for now, see ya!