Click here to Skip to main content
15,881,690 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

looking for a way to convert "10/10.00" to decimal.
Posted
Comments
[no name] 29-Apr-13 17:47pm    
Well the obvious thing to do would be to simply do the math....
bobb024 29-Apr-13 18:00pm    
I am looking to compare to numbers and currently I have

if (FractionToDouble(parse.salePriceValue) > FractionToDouble(parse.regularPriceValue))
{
rejectedList.Add(data[0] + ", Regular Price cannot be Higher than Sale Price");
createSign = false;
}


public static double FractionToDouble(string fraction)
{
double result;

if (double.TryParse(fraction, out result))
{
return result;
}

string[] split = fraction.Split(new char[] { ' ', '/' });

if (split.Length == 2 || split.Length == 3)
{
int a, b;

if (int.TryParse(split[0], out a) && int.TryParse(split[1], out b))
{
if (split.Length == 2)
{
return (double)a / b;
}

int c;

if (int.TryParse(split[2], out c))
{
return a + (double)b / c;
}
}
}

throw new FormatException("Not a valid fraction.");
}
Dave Kreskowiak 29-Apr-13 18:37pm    
And the problem would be ...... ?

1 solution

C#
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)
       {
           // Use this method is used to read all bytes from a stream.
           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;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900