import re
import math


def convertDollarString(ds):
    """Convert to numerical dollar amount. This can be a dollar string without
       magnitude suffix (e.g., "$100,000"), or a dollar string WITH a magnitude
       suffix (e.g., "$123k"). Supported suffixes include "k", "M", and "B".
    """
    suffix = ds[-1]
    assert ds[0] == "$"
    if re.match(r"^\d$", suffix):
        # last character is numeric, strip of commas and convert directly
        ds = re.sub(r",", "", ds)
        dv = float(ds[1:])
    else:
        # last character should be a suffix (and have no commas)
        ds = ds[1:-1]
        mag = 0
        if suffix == "k":
            mag = 3
        elif suffix == "M":
            mag = 6
        elif suffix == "B":
            mag = 9
        else:
            raise Exception("Unsupported financial magnitude suffix '%s'" % suffix)
        dv = float(ds) * math.pow(10, mag)
    return dv