在asp.net的中,如何用分数表示数值?
发布网友
发布时间:2022-05-16 23:01
我来回答
共3个回答
热心网友
时间:2022-04-27 16:32
真服了你了。。。。。给你再加个转换的方法总行了吧??!
public class Fraction
{
ulong divisor;
ulong dividend;
/// <summary>
/// 分母
/// </summary>
public ulong Divisor
{
get
{
return divisor;
}
set
{
if (value != 0)
divisor = value;
else
throw new Exception("分母不能为0.");
}
}
/// <summary>
/// 分子
/// </summary>
public ulong Dividend
{
get
{
return dividend;
}
set
{
dividend = value;
}
}
public double Value
{
get
{
return (double)Dividend / (double)Divisor;
}
}
public override string ToString()
{
return dividend.ToString() + "/" + divisor.ToString();
}
/// <summary>
/// 构造函数
/// </summary>
public Fraction() { }
/// <summary>
/// 将string转成Fraction
/// </summary>
/// <param name="numberString">分数字符串</param>
/// <returns> Fraction 实例,如果转换失败,返回null</returns>
static public Fraction ConvertToFraction(string numberString)
{
if (string.IsNullOrEmpty(numberString))
return null;
string[] result = numberString.Split('/');
if (result.Length != 2)
return null;
ulong divisor, dividend;
try
{
divisor = Convert.ToUInt64(result[1]);
dividend = Convert.ToUInt64(result[0]);
}
catch
{
return null;
}
if (divisor == 0)
return null;
Fraction fraction = new Fraction();
fraction.dividend = dividend;
fraction.divisor = divisor;
return fraction;
}
/// <summary>
/// 加法
/// </summary>
/// <param name="fraction"></param>
/// <returns></returns>
public Fraction Add(Fraction fraction)
{
Fraction result = new Fraction();
result.Divisor = minCommonMultiple(this.Divisor, fraction.Divisor);
result.Dividend = this.Dividend * result.Divisor / this.Divisor + fraction.Dividend * result.Divisor / fraction.Divisor;
return Simplify(result);
}
/// <summary>
/// 减法
/// </summary>
/// <param name="fraction"></param>
/// <returns></returns>
public Fraction Subtract(Fraction fraction)
{
Fraction result = new Fraction();
result.Divisor = minCommonMultiple(this.Divisor, fraction.Divisor);
result.Dividend = this.Dividend * result.Divisor / this.Divisor - fraction.Dividend *
result.Divisor / fraction.Divisor;
return Simplify(result);
}
/// <summary>
/// 乘法
/// </summary>
/// <param name="fraction"></param>
/// <returns></returns>
public Fraction Multiply(Fraction fraction)
{
Fraction result = new Fraction();
result.Divisor = this.Divisor * fraction.Divisor;
result.Dividend = this.Dividend * fraction.Dividend;
return Simplify(result);
}
/// <summary>
/// 除法
/// </summary>
/// <param name="fraction"></param>
/// <returns></returns>
public Fraction Divide(Fraction fraction)
{
Fraction result = new Fraction();
result.Divisor = this.Divisor * fraction.Dividend;
result.Dividend = this.Dividend * fraction.Divisor;
return Simplify(result);
}
/// <summary>
/// 最小公倍数
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
private static ulong minCommonMultiple(ulong num1, ulong num2)
{
ulong mcd = maxCommonDivisor(num1, num2);
if (mcd == 1)
return num1 * num2;
return num1 * num2 / mcd;
}
/// <summary>
/// 最大公约数
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
private static ulong maxCommonDivisor(ulong num1, ulong num2)
{
ulong min, max;
if (num1 < num2)
{
min = num1;
max = num2;
}
else
{
min = num2;
max = num1;
}
double tmp = ((double)max / (double)min);
ulong tmp2 = (ulong)tmp;
if (tmp == (double)tmp2)
{
return tmp2;
}
return 1;
}
/// <summary>
/// 化简
/// </summary>
/// <param name="fraction"></param>
/// <returns></returns>
static public Fraction Simplify(Fraction fraction)
{
ulong div = maxCommonDivisor(fraction.Divisor, fraction.Dividend);
fraction.Divisor /= div;
fraction.Dividend /= div;
return fraction;
}
}
示例:
string text1 = "1/3";
string text2 = "1/4";
Fraction a = Fraction.ConvertToFraction(text1);
Fraction b = Fraction.ConvertToFraction(text2);
if (a != null && b != null)
{
Fraction c = a.Add(b);
Console.WriteLine(c.Value);
Console.WriteLine(c.ToString());
}
else
{
Console.WriteLine("Convert is invalid.");
}
你实际使用中,把text1 ,text2换成你的文本值,把c的ToString()赋值给你显示结果的文本,总OK了吧??
热心网友
时间:2022-04-27 17:50
public class MyTextBox : TextBox
{
public void SetValue(double value)
{
//在这里将数字转换为分数:
// value=1.5
return "1/5";
}
}
热心网友
时间:2022-04-27 19:25
没有必要吧?碰上一些麻烦的数就倒霉了,比如231/700,这个分数的大小不是很直观了。