• 0

[Any] Get From Decimal -> Numerator/Denomininator


Question

I think there's a .NET Library for dealing with fractions and/or decimals. I don't currently know of any, if you would like to share them with me I'd appreciate it.

I have a Class type Rational Numbers, and I want to convert a decimal value to Rational. How can I get the Numerator and Denominator from a Decimal Number in an algorithm? My only idea as of this time, is using a loop, convert to string and get location of decimal using string[x], it's rather inefficient.

The purpose of my overall program will be to practice Operator Overloading, which I've omitted in my paste.

This program is in C#

class Rational

{

  private int num=0;

  private int deno=1;

  private int gcd (int anum,int adeno)

  {

  int temp,gcd=1;

  if (anum<=adeno)

    temp=anum;

  else temp=adeno;

  for (int i=1; i<=temp; ++i)

  {

    if (anum%i==0 && adeno%i==0)

    gcd=i;

  }

  return gcd;

  }

  public Rational()

  {num=0;deno=1;}

  public Rational(int num,int deno)

  {

  int agcd=gcd(num,deno);

  this.num=num/agcd;

  this.deno=deno/agcd;

  }

  public Rational(Rational r)

  {num=r.num;deno=r.deno;}

  public Rational(int num)

  {this.num=num;deno=1;}

public Rational (double adecimalnumber) //Here is what I need help with

{ }

}

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

My skills as regards C# is not really great, but perhaps a friend of mine can help. Go and have a look at his blog and drop him a line. He has been at the C# things for a while now, and is really quite sharp himself when it comes to these sort of issues. Here is the link: Deon's Blog

Link to comment
Share on other sites

  • 0

I'm sure there's an easier way than a loop to convert to a string in C#, but since I don't really use C#, I'm not sure what it is. I'm sure a google search will turn up something. And I'm also sure it has a locationOf(char x) type of thing too. Then you just subtract that from the total length of the string.

Link to comment
Share on other sites

  • 0
I'm sure there's an easier way than a loop to convert to a string in C#, but since I don't really use C#, I'm not sure what it is.  I'm sure a google search will turn up something.  And I'm also sure it has a locationOf(char x) type of thing too.  Then you just subtract that from the total length of the string.

hi kjordan2001, yes, lemme demonstrate;

string mystring = "Fox";

Console.WriteLine(mystring[2]); // this will print "o" i think unless the first index is 0, then it will print x, either way, C# is treating all strings similar to an //array (of chars I guess).

double decimalnumber = 5.3421;

Console.Writeline(Convert.ToString(decimalnumber)[2]); //should print decimal

Ok, next is the problem occuring

decimalnumber = .324;

Console.WriteLine(Convert.ToString(decimalnumber).Remove(1,1)); ok, this may drive one nuts to read, but what its doing is reading the decimal index for

// my variable, and then attempting to remove it. The Remove() takes 2 arguments, starting Index to begin removal, and How many indexes ++ to remove.

//problem is, the system is seeing it as 0.324, and regardless if i try to remove the 2 spot, instead of the 1, it doesn't work there is always a preceding 0.

I dont wanna show the numerator as 0324, i want 324

Here's the good news, I found exactly what I want on the web;

The following information was retrieved from http://www.boyet.com/Articles/GcdfractionClass.html, julian m bucknall

// explicit conversion from double to Fraction

        static public explicit operator Fraction(double value) {

            // uses the Continued Fraction algorithm

            bool isNegative = (value < 0.0);

            value = Math.Abs(value);

            int prevPrevN = 0;         

            int prevPrevD = 1;         

            int prevN = 1;

            int prevD = 0;

            int a = (int) Math.Floor(value);

            double x = value - Math.Floor(value);

            int n = (a * prevN) + prevPrevN;

            int d = (a * prevD) + prevPrevD;

            double error = Math.Abs(value - (n / d));

            while (error > 1e-6) { A million dollars to whomever can explain what '1e-6' is

                prevPrevN = prevN;

                prevPrevD = prevD;

                prevD = d;

                prevN = n;

               

                x = 1.0 / x;

                a = (int) Math.Floor(x);

                x = x - Math.Floor(x);;

                n = (a * prevN) + prevPrevN;

                d = (a * prevD) + prevPrevD;

                error = Math.Abs(value - ((double) n / d));

            }

            if (isNegative)

                n = -n;

            return new Fraction(n, d);

        }

Bad News, is it's not my code AND there Must be a simpler way

I'm going to try again a little later today.

hi toejam,

Your friends' site is impressive, I am going to examine this "Symmetric Key Encryption using Rijndael and C#", something of high interest in my heart.

I will keep this thread updated

Link to comment
Share on other sites

  • 0

1e-6 = 0.000001

"E" or "e" ("exponent") is used to signify *10^y (i.e. multiply by 10 raised to the power of "y").

So, 1E4 = 1*(10^4) = 1*(10*10*10*10) = 1*1000 = 1000. You can use negative numbers as well:

1e-6 = 1/(10*10*10*10*10*10) = 1/1000000 = 0.000001.

A simpler way of doing it is just to move the decimal point by the number indicated in the exponent:

1E4 = 1, moving the decimal point right by 4 = 1000

1E-6 = 1, moving the decimal point left by 6 = .000001

That'll be $1e6, please.

Link to comment
Share on other sites

  • 0

decimalnumber = .324;

Console.WriteLine(Convert.ToString(decimalnumber).Remove(1,1)); ok, this may drive one nuts to read, but what its doing is reading the decimal index for

// my variable, and then attempting to remove it. The Remove() takes 2 arguments, starting Index to begin removal, and How many indexes ++ to remove.

//problem is, the system is seeing it as 0.324, and regardless if i try to remove the 2 spot, instead of the 1, it doesn't work there is always a preceding 0.

I dont wanna show the numerator as 0324, i want 324

String s=Convert.ToString(decimalnumber);

if (s.charAt(0) == '0') {

s = s.Remove(0,2);

} else {

s = s.Remove(1,1);

}

Replace with the C# equivalent of the Java method charAt.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.