- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想在 C# 中将 double 值显示为分数,我该怎么做?
谢谢
最佳答案
试试这个用于 C# 的 Fraction 类。
/*
* Author: Syed Mehroz Alam
* Email: smehrozalam@yahoo.com
* URL: Programming Home "http://www.geocities.com/smehrozalam/"
* Date: 6/15/2004
* Time: 10:54 AM
*
*/
using System;
namespace Mehroz
{
/// <summary>
/// Classes Contained:
/// Fraction
/// FractionException
/// </summary>
/// Class name: Fraction
/// Developed by: Syed Mehroz Alam
/// Email: smehrozalam@yahoo.com
/// URL: Programming Home "http://www.geocities.com/smehrozalam/"
/// Version: 2.0
///
/// What's new in version 2.0:
/// * Changed Numerator and Denominator from Int32(integer) to Int64(long) for increased range
/// * renamed ConvertToString() to (overloaded) ToString()
/// * added the capability of detecting/raising overflow exceptions
/// * Fixed the bug that very small numbers e.g. 0.00000001 could not be converted to fraction
/// * Other minor bugs fixed
///
/// What's new in version 2.1
/// * overloaded user-defined conversions to/from Fractions
///
///
/// Properties:
/// Numerator: Set/Get value for Numerator
/// Denominator: Set/Get value for Numerator
/// Value: Set an integer value for the fraction
///
/// Constructors:
/// no arguments: initializes fraction as 0/1
/// (Numerator, Denominator): initializes fraction with the given numerator and denominator values
/// (integer): initializes fraction with the given integer value
/// (long): initializes fraction with the given long value
/// (double): initializes fraction with the given double value
/// (string): initializes fraction with the given string value
/// the string can be an in the form of and integer, double or fraction.
/// e.g it can be like "123" or "123.321" or "123/456"
///
/// Public Methods (Description is given with respective methods' definitions)
/// (override) string ToString(Fraction)
/// Fraction ToFraction(string)
/// Fraction ToFraction(double)
/// double ToDouble(Fraction)
/// Fraction Duplicate()
/// Fraction Inverse(integer)
/// Fraction Inverse(Fraction)
/// ReduceFraction(Fraction)
/// Equals(object)
/// GetHashCode()
///
/// Private Methods (Description is given with respective methods' definitions)
/// Initialize(Numerator, Denominator)
/// Fraction Negate(Fraction)
/// Fraction Add(Fraction1, Fraction2)
///
/// Overloaded Operators (overloaded for Fractions, Integers and Doubles)
/// Unary: -
/// Binary: +,-,*,/
/// Relational and Logical Operators: ==,!=,<,>,<=,>=
///
/// Overloaded user-defined conversions
/// Implicit: From double/long/string to Fraction
/// Explicit: From Fraction to double/string
/// </summary>
public class Fraction
{
/// <summary>
/// Class attributes/members
/// </summary>
long m_iNumerator;
long m_iDenominator;
/// <summary>
/// Constructors
/// </summary>
public Fraction()
{
Initialize(0,1);
}
public Fraction(long iWholeNumber)
{
Initialize(iWholeNumber, 1);
}
public Fraction(double dDecimalValue)
{
Fraction temp=ToFraction(dDecimalValue);
Initialize(temp.Numerator, temp.Denominator);
}
public Fraction(string strValue)
{
Fraction temp=ToFraction(strValue);
Initialize(temp.Numerator, temp.Denominator);
}
public Fraction(long iNumerator, long iDenominator)
{
Initialize(iNumerator, iDenominator);
}
/// <summary>
/// Internal function for constructors
/// </summary>
private void Initialize(long iNumerator, long iDenominator)
{
Numerator=iNumerator;
Denominator=iDenominator;
ReduceFraction(this);
}
/// <summary>
/// Properites
/// </summary>
public long Denominator
{
get
{ return m_iDenominator; }
set
{
if (value!=0)
m_iDenominator=value;
else
throw new FractionException("Denominator cannot be assigned a ZERO Value");
}
}
public long Numerator
{
get
{ return m_iNumerator; }
set
{ m_iNumerator=value; }
}
public long Value
{
set
{ m_iNumerator=value;
m_iDenominator=1; }
}
/// <summary>
/// The function returns the current Fraction object as double
/// </summary>
public double ToDouble()
{
return ( (double)this.Numerator/this.Denominator );
}
/// <summary>
/// The function returns the current Fraction object as a string
/// </summary>
public override string ToString()
{
string str;
if ( this.Denominator==1 )
str=this.Numerator.ToString();
else
str=this.Numerator + "/" + this.Denominator;
return str;
}
/// <summary>
/// The function takes an string as an argument and returns its corresponding reduced fraction
/// the string can be an in the form of and integer, double or fraction.
/// e.g it can be like "123" or "123.321" or "123/456"
/// </summary>
public static Fraction ToFraction(string strValue)
{
int i;
for (i=0;i<strValue.Length;i++)
if (strValue[i]=='/')
break;
if (i==strValue.Length) // if string is not in the form of a fraction
// then it is double or integer
return ( Convert.ToDouble(strValue));
//return ( ToFraction( Convert.ToDouble(strValue) ) );
// else string is in the form of Numerator/Denominator
long iNumerator=Convert.ToInt64(strValue.Substring(0,i));
long iDenominator=Convert.ToInt64(strValue.Substring(i+1));
return new Fraction(iNumerator, iDenominator);
}
/// <summary>
/// The function takes a floating point number as an argument
/// and returns its corresponding reduced fraction
/// </summary>
public static Fraction ToFraction(double dValue)
{
try
{
checked
{
Fraction frac;
if (dValue%1==0) // if whole number
{
frac=new Fraction( (long) dValue );
}
else
{
double dTemp=dValue;
long iMultiple=1;
string strTemp=dValue.ToString();
while ( strTemp.IndexOf("E")>0 ) // if in the form like 12E-9
{
dTemp*=10;
iMultiple*=10;
strTemp=dTemp.ToString();
}
int i=0;
while ( strTemp[i]!='.' )
i++;
int iDigitsAfterDecimal=strTemp.Length-i-1;
while ( iDigitsAfterDecimal>0 )
{
dTemp*=10;
iMultiple*=10;
iDigitsAfterDecimal--;
}
frac=new Fraction( (int)Math.Round(dTemp) , iMultiple );
}
return frac;
}
}
catch(OverflowException)
{
throw new FractionException("Conversion not possible due to overflow");
}
catch(Exception)
{
throw new FractionException("Conversion not possible");
}
}
/// <summary>
/// The function replicates current Fraction object
/// </summary>
public Fraction Duplicate()
{
Fraction frac=new Fraction();
frac.Numerator=Numerator;
frac.Denominator=Denominator;
return frac;
}
/// <summary>
/// The function returns the inverse of a Fraction object
/// </summary>
public static Fraction Inverse(Fraction frac1)
{
if (frac1.Numerator==0)
throw new FractionException("Operation not possible (Denominator cannot be assigned a ZERO Value)");
long iNumerator=frac1.Denominator;
long iDenominator=frac1.Numerator;
return ( new Fraction(iNumerator, iDenominator));
}
/// <summary>
/// Operators for the Fraction object
/// includes -(unary), and binary opertors such as +,-,*,/
/// also includes relational and logical operators such as ==,!=,<,>,<=,>=
/// </summary>
public static Fraction operator -(Fraction frac1)
{ return ( Negate(frac1) ); }
public static Fraction operator +(Fraction frac1, Fraction frac2)
{ return ( Add(frac1 , frac2) ); }
public static Fraction operator +(int iNo, Fraction frac1)
{ return ( Add(frac1 , new Fraction(iNo) ) ); }
public static Fraction operator +(Fraction frac1, int iNo)
{ return ( Add(frac1 , new Fraction(iNo) ) ); }
public static Fraction operator +(double dbl, Fraction frac1)
{ return ( Add(frac1 , Fraction.ToFraction(dbl) ) ); }
public static Fraction operator +(Fraction frac1, double dbl)
{ return ( Add(frac1 , Fraction.ToFraction(dbl) ) ); }
public static Fraction operator -(Fraction frac1, Fraction frac2)
{ return ( Add(frac1 , -frac2) ); }
public static Fraction operator -(int iNo, Fraction frac1)
{ return ( Add(-frac1 , new Fraction(iNo) ) ); }
public static Fraction operator -(Fraction frac1, int iNo)
{ return ( Add(frac1 , -(new Fraction(iNo)) ) ); }
public static Fraction operator -(double dbl, Fraction frac1)
{ return ( Add(-frac1 , Fraction.ToFraction(dbl) ) ); }
public static Fraction operator -(Fraction frac1, double dbl)
{ return ( Add(frac1 , -Fraction.ToFraction(dbl) ) ); }
public static Fraction operator *(Fraction frac1, Fraction frac2)
{ return ( Multiply(frac1 , frac2) ); }
public static Fraction operator *(int iNo, Fraction frac1)
{ return ( Multiply(frac1 , new Fraction(iNo) ) ); }
public static Fraction operator *(Fraction frac1, int iNo)
{ return ( Multiply(frac1 , new Fraction(iNo) ) ); }
public static Fraction operator *(double dbl, Fraction frac1)
{ return ( Multiply(frac1 , Fraction.ToFraction(dbl) ) ); }
public static Fraction operator *(Fraction frac1, double dbl)
{ return ( Multiply(frac1 , Fraction.ToFraction(dbl) ) ); }
public static Fraction operator /(Fraction frac1, Fraction frac2)
{ return ( Multiply( frac1 , Inverse(frac2) ) ); }
public static Fraction operator /(int iNo, Fraction frac1)
{ return ( Multiply( Inverse(frac1) , new Fraction(iNo) ) ); }
public static Fraction operator /(Fraction frac1, int iNo)
{ return ( Multiply( frac1 , Inverse(new Fraction(iNo)) ) ); }
public static Fraction operator /(double dbl, Fraction frac1)
{ return ( Multiply( Inverse(frac1) , Fraction.ToFraction(dbl) ) ); }
public static Fraction operator /(Fraction frac1, double dbl)
{ return ( Multiply( frac1 , Fraction.Inverse( Fraction.ToFraction(dbl) ) ) ); }
public static bool operator ==(Fraction frac1, Fraction frac2)
{ return frac1.Equals(frac2); }
public static bool operator !=(Fraction frac1, Fraction frac2)
{ return ( !frac1.Equals(frac2) ); }
public static bool operator ==(Fraction frac1, int iNo)
{ return frac1.Equals( new Fraction(iNo)); }
public static bool operator !=(Fraction frac1, int iNo)
{ return ( !frac1.Equals( new Fraction(iNo)) ); }
public static bool operator ==(Fraction frac1, double dbl)
{ return frac1.Equals( new Fraction(dbl)); }
public static bool operator !=(Fraction frac1, double dbl)
{ return ( !frac1.Equals( new Fraction(dbl)) ); }
public static bool operator<(Fraction frac1, Fraction frac2)
{ return frac1.Numerator * frac2.Denominator < frac2.Numerator * frac1.Denominator; }
public static bool operator>(Fraction frac1, Fraction frac2)
{ return frac1.Numerator * frac2.Denominator > frac2.Numerator * frac1.Denominator; }
public static bool operator<=(Fraction frac1, Fraction frac2)
{ return frac1.Numerator * frac2.Denominator <= frac2.Numerator * frac1.Denominator; }
public static bool operator>=(Fraction frac1, Fraction frac2)
{ return frac1.Numerator * frac2.Denominator >= frac2.Numerator * frac1.Denominator; }
/// <summary>
/// overloaed user defined conversions: from numeric data types to Fractions
/// </summary>
public static implicit operator Fraction(long lNo)
{ return new Fraction(lNo); }
public static implicit operator Fraction(double dNo)
{ return new Fraction(dNo); }
public static implicit operator Fraction(string strNo)
{ return new Fraction(strNo); }
/// <summary>
/// overloaed user defined conversions: from fractions to double and string
/// </summary>
public static explicit operator double(Fraction frac)
{ return frac.ToDouble(); }
public static implicit operator string(Fraction frac)
{ return frac.ToString(); }
/// <summary>
/// checks whether two fractions are equal
/// </summary>
public override bool Equals(object obj)
{
Fraction frac=(Fraction)obj;
return ( Numerator==frac.Numerator && Denominator==frac.Denominator);
}
/// <summary>
/// returns a hash code for this fraction
/// </summary>
public override int GetHashCode()
{
return ( Convert.ToInt32((Numerator ^ Denominator) & 0xFFFFFFFF) ) ;
}
/// <summary>
/// internal function for negation
/// </summary>
private static Fraction Negate(Fraction frac1)
{
long iNumerator=-frac1.Numerator;
long iDenominator=frac1.Denominator;
return ( new Fraction(iNumerator, iDenominator) );
}
/// <summary>
/// internal functions for binary operations
/// </summary>
private static Fraction Add(Fraction frac1, Fraction frac2)
{
try
{
checked
{
long iNumerator=frac1.Numerator*frac2.Denominator + frac2.Numerator*frac1.Denominator;
long iDenominator=frac1.Denominator*frac2.Denominator;
return ( new Fraction(iNumerator, iDenominator) );
}
}
catch(OverflowException)
{
throw new FractionException("Overflow occurred while performing arithemetic operation");
}
catch(Exception)
{
throw new FractionException("An error occurred while performing arithemetic operation");
}
}
private static Fraction Multiply(Fraction frac1, Fraction frac2)
{
try
{
checked
{
long iNumerator=frac1.Numerator*frac2.Numerator;
long iDenominator=frac1.Denominator*frac2.Denominator;
return ( new Fraction(iNumerator, iDenominator) );
}
}
catch(OverflowException)
{
throw new FractionException("Overflow occurred while performing arithemetic operation");
}
catch(Exception)
{
throw new FractionException("An error occurred while performing arithemetic operation");
}
}
/// <summary>
/// The function returns GCD of two numbers (used for reducing a Fraction)
/// </summary>
private static long GCD(long iNo1, long iNo2)
{
// take absolute values
if (iNo1 < 0) iNo1 = -iNo1;
if (iNo2 < 0) iNo2 = -iNo2;
do
{
if (iNo1 < iNo2)
{
long tmp = iNo1; // swap the two operands
iNo1 = iNo2;
iNo2 = tmp;
}
iNo1 = iNo1 % iNo2;
} while (iNo1 != 0);
return iNo2;
}
/// <summary>
/// The function reduces(simplifies) a Fraction object by dividing both its numerator
/// and denominator by their GCD
/// </summary>
public static void ReduceFraction(Fraction frac)
{
try
{
if (frac.Numerator==0)
{
frac.Denominator=1;
return;
}
long iGCD=GCD(frac.Numerator, frac.Denominator);
frac.Numerator/=iGCD;
frac.Denominator/=iGCD;
if ( frac.Denominator<0 ) // if -ve sign in denominator
{
//pass -ve sign to numerator
frac.Numerator*=-1;
frac.Denominator*=-1;
}
} // end try
catch(Exception exp)
{
throw new FractionException("Cannot reduce Fraction: " + exp.Message);
}
}
} //end class Fraction
/// <summary>
/// Exception class for Fraction, derived from System.Exception
/// </summary>
public class FractionException : Exception
{
public FractionException() : base()
{}
public FractionException(string Message) : base(Message)
{}
public FractionException(string Message, Exception InnerException) : base(Message, InnerException)
{}
} //end class FractionException
} //end namespace Mehroz
正如@Tillito 在评论中指出的那样,我仔细检查了以下代码:
using System.IO;
using System;
using Mehroz;
class Program
{
static void Main()
{
double d = .5;
string str = new Fraction(d).ToString();
Console.WriteLine(str);
}
}
哪些输出:
1/2
关于c# - 在 C# 中将 double 转换为分数作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7564906/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!