public static decimal FractionToDouble(string fraction)
{
decimal result;
if (decimal.TryParse(fraction, out result))
{
return result;
}
string[] split = fraction.Split(new char[] { ' ', '/' });
if (split.Length == 2 || split.Length == 3)
{
decimal a, b;
if (decimal.TryParse(split[0], out a) && decimal.TryParse(split[1], out b))
{
if (split.Length == 2)
{
return (decimal)b / a;
}
int c;
if (int.TryParse(split[2], out c))
{
return a + (decimal)b / c;
}
}
}
throw new FormatException("Not a valid fraction.");
}
public static int ReadAllBytesFromStream(Stream stream, byte[] buffer)
{
int offset = 0;
int totalCount = 0;
while (true)
{
int bytesRead = stream.Read(buffer, offset, 100);
if (bytesRead == 0)
{
break;
}
offset += bytesRead;
totalCount += bytesRead;
}
return totalCount;
}