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
Question
+Fulcrum Subscriber¹
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#
Link to comment
Share on other sites
6 answers to this question
Recommended Posts