Safely Convert String To Double With Rounding

By Robbe Morris

Some very old code I found upgraded a tad to be current. Used to safely convert a string to a double rounded off to the nearest decimal.

public static double ToDouble(string originalValue, int roundPlaces)
{

   try
  {

    if (String.IsNullOrEmpty(originalValue)) return 0;

    if (roundPlaces < 100) return System.Math.Round(System.Double.Parse(originalValue), roundPlaces);

    return System.Double.Parse(originalValue);

  }
  catch { }

  return 0;
}

Safely Convert String To Double With Rounding  (1034 Views)