4

I could probably do this in a series of steps by looking at the last character of the string and based on what it is send it to a particular function to have the conversion made. But I am just wondering if there is any easier way to do the following.

For example I have a string that might say something like 23.44M or 5.23B, the M and B obviously stand for "Million" or "Billion", I want to convert that string into the number it is representing but just not sure of the most efficient way to do this. Looking for some ideas. Thanks

4
  • 1
    There is no built-in function in .NET that implements this. The approach you describe sounds sensible. Commented Dec 20, 2011 at 16:27
  • 1
    Read the last character in the string, remove the character, then cat what you actualy want Commented Dec 20, 2011 at 16:28
  • you can write your own Switch/ Case statement and custom methods to easily handle this Commented Dec 20, 2011 at 16:30
  • Are million and billion the only suffixes you have? Can there be other characters in the string (e.g. spaces)? In the end, the method you'll write will depend mostly on what the constraints on the input string are. Commented Dec 20, 2011 at 16:31

6 Answers 6

6

I would build a Dictionary, and populate it with key value pairs such as (B, 1,000,000,000) and (M, 1,000,000). Take the character off the end, look up the value for that character, and multiply.

Sign up to request clarification or add additional context in comments.

Comments

1

Something along these lines work for you?

double num = Double.parse(inputStr.Remove(inputStr.Count-1));
char lastChar = inputStr.Last();
switch(lastChar)
{
   case 'M':
      num *= 1000000;
      break;
   case 'B':
      num *= 1000000000;
      break;
}

If the format of your input can vary, you would have to add additional logic, including protecting against an illegal Parse().

Comments

0
/// <summary>
/// Gets the value.
/// </summary>
/// <param name="number">The number.</param>
/// <returns></returns>
public static decimal GetValue(string number)
{
    char unit = number.Last();
    string num = number.Remove(number.Length - 1, 1);

    decimal multiplier;
    switch (unit)
    {
        case 'M':
        case 'm':
            multiplier = 1000000; break;

        default:
            multiplier = 1; break;
    }

    return decimal.Parse(num) * multiplier;
}

2 Comments

Thanks for all the great responses. This method right here works great!
@Ron Would be nice if you accepted one of the solutions then.
0

As a nerdy alternative to the answer by @Smelch you could populate a String constant POSTFIXES, such as "xHKxxMxxBxxTxxQ" and multiply your base by Math.Pow(10,POSTFIXES.LastIndexOf(...)+1)

Comments

0

your best bet is probably to use a regex (MSDN docs here) you could group on (this is NOT regex syntax) [numbers][char] then you have 2 groups, the first would be your number, the second would be your "factor" character

then, convert the first with double.TryParse(), and run your second through a switch or a set of if statements to multiply as needed to get your end result.

Comments

0

Use a dictionary to map chars to multipliers instead of a switch statement and use upper case only:

public static decimal GetValue(string number)
{
    return decimal.Parse(number.Remove(number.Length - 1, 1)) *  _multipliers[number.Last().ToUpper()];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.