# 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");
}
}```