Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# decimal conversion
Hello,
 
looking for a way to convert "10/10.00" to decimal.
Posted 29-Apr-13 12:42pm
bobb024317
Comments
ThePhantomUpvoter at 29-Apr-13 17:47pm
   
Well the obvious thing to do would be to simply do the math....
bobb024 at 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 at 29-Apr-13 18:37pm
   
And the problem would be ...... ?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
       }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100