在 C# 中计算比率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/527860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate a Ratio in C#
提问by GateKiller
I thought this would be simple, but searching Google didn't seem to help.
我认为这很简单,但搜索谷歌似乎没有帮助。
I'm basically trying to write a function which will return a ratio as a string (eg 4:3) when supplies with two integers (eg 800 and 600).
我基本上是在尝试编写一个函数,当提供两个整数(例如 800 和 600)时,该函数将返回一个比率作为字符串(例如 4:3)。
string GetRatio(Int A, Int B) {
// Code I'm looking for
return Ratio;
}
采纳答案by Konrad Rudolph
You can simplify fractions by dividing numerator and denominator by their GCD:
您可以通过将分子和分母除以它们的GCD来简化分数:
var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)
And a very basic function for calculating the GCD, using the Euclidean algorithm:
以及使用欧几里得算法计算 GCD 的一个非常基本的函数:
static int GCD(int a, int b) {
return b == 0 ? Math.Abs(a) : GCD(b, a % b);
}
回答by Tomas Pajonk
Are you basically trying to get the greatest common denominator - GCD for the two numbers and then dividing them by that and thus getting your string ?
你基本上是想得到两个数字的最大公分母 - GCD,然后除以它们,从而得到你的字符串吗?
I.e: 800 : 600 ; the greatest common denominator = 200 thus 4:3.
即:800:600;最大公分母 = 200 因此为 4:3。
This would be able to deal with all integer numbers. Sorry for not sending the code, but I think that from this on it should be simple enough.
这将能够处理所有整数。抱歉没有发送代码,但我认为从这里开始它应该足够简单。
public int GCD(int a, int b)
{
while (a != 0 && b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}
if (a == 0)
return b;
else
return a;
}
// Using Konrad's code:
var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)
回答by kquinn
Other commentators have given good solutions for integers; if you really have to deal with floating-point values, though, you'll need something else. In general, two real numbers won't have a clean ratio that can be prettily printed; what you want is the closest rational approximation. Probably the best way to go about finding thatis just to compute the continued fraction expansion of the quotient; Mark Dominus gives a good introduction to those on his blog.
其他评论者对整数给出了很好的解决方案;但是,如果您真的必须处理浮点值,那么您将需要其他东西。一般来说,两个实数不会有一个干净的比率,可以漂亮地打印出来;你想要的是最接近的有理近似。也许去寻找最好的办法是就是计算商的连分数扩展; Mark Dominus在他的博客上很好地介绍了这些内容。
回答by joel.neely
Having played with such things in the past, I'll just add that dealing with signed values can get ugly. Let me suggest that the simplest way to handle signed values is to apply Konrad's approach to the absolute valuesof your original numbers, then prepend a '-' to the resulting string if the original values have different signs.
过去玩过这些东西,我只想补充一点,处理带符号的值可能会变得丑陋。让我建议处理有符号值的最简单方法是将 Konrad 的方法应用于原始数字的绝对值,然后如果原始值具有不同的符号,则在结果字符串前加上“-”。
Using this approach, the Greatest Common Divisor of -100 and -35 is 5, for a ratio of 20:7. If the original inputs had been either of the pairs (-100 and 35) or (100 and -35), you'd still get a GCD of 5, and an initial result of 20:7, but the final answer would be -20:7 (i.e. standardized form regardless of which input was negative, just as both -6/2 and 6/-2 = -3).
使用这种方法,-100 和 -35 的最大公约数是 5,比率为 20:7。如果原始输入是 (-100 和 35) 或 (100 和 -35) 对中的任何一个,您仍然会得到 5 的 GCD,以及 20:7 的初始结果,但最终答案是 - 20:7(即标准化形式,无论哪个输入为负,就像 -6/2 和 6/-2 = -3 一样)。