iphone - 在Objective-C中将小数转换为分数?

我正在尝试将小数点后的所有内容显示为分数。在如何实现这一点上,objective-c 找不到太多东西。我正在使用 double 来格式化变量,不确定这是否重要。这就是我为我的答案输出格式化的方式:[theTextField setText:[NSString stringWithFormat:@"%f''", (myVariable)]]; 这显示为十进制,但真的喜欢它作为一个整数和分数(即)7 1/2 而不是 7.5000。提前谢谢你!

更新:2011 年 5 月 13 日

嗯,我让它显示 7 1/16,但数学上的东西是关闭的。因为即使改变被除的值,它也不会从 1/16 改变。我在哪里错了?如果有人真的可以让它正常工作,请完全发布它是如何完成的。这应该很简单,但又不会太耗时。请完整发布它是如何完成的。谢谢。

更新: 如果此答案对您不起作用,请查看我的其他帖子,这有效! Convert decimals to fractions

最佳答案

Objective-C 基本上使用纯 C 语言进行所有原始数学运算。

也就是说,您将在其他问题的答案中找到所有必要的信息(以及 C 代码):

How to convert floats to human-readable fractions?

(特别是 this answer 包含实际的 C 代码。)

这里是该算法的快速 c 函数包装器:

typedef struct {
    long nominator;
    long denominator;
    double error;
} Fraction;

/*
 * Find rational approximation to given real number
 * David Eppstein / UC Irvine / 8 Aug 1993
 *
 * With corrections from Arno Formella, May 2008
 * Function wrapper by Regexident, April 2011
 *
 * usage: fractionFromReal(double realNumber, long maxDenominator)
 *   realNumber: is real number to approx
 *   maxDenominator: is the maximum denominator allowed
 *
 * based on the theory of continued fractions
 * if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...)))
 * then best approximation is found by truncating this series
 * (with some adjustments in the last term).
 *
 * Note the fraction can be recovered as the first column of the matrix
 *  ( a1 1 ) ( a2 1 ) ( a3 1 ) ...
 *  ( 1  0 ) ( 1  0 ) ( 1  0 )
 * Instead of keeping the sequence of continued fraction terms,
 * we just keep the last partial product of these matrices.
 */
Fraction fractionFromReal(double realNumber, long maxDenominator) {
   double atof();
   int atoi();
   void exit();

   long m[2][2];
   double startx;
   long ai;

   startx = realNumber;

   // initialize matrix:
   m[0][0] = m[1][1] = 1;
   m[0][1] = m[1][0] = 0;

   // loop finding terms until denom gets too big:
   while (m[1][0] *  (ai = (long)realNumber) + m[1][1] <= maxDenominator) {
       long t;
       t = m[0][0] * ai + m[0][1];
       m[0][1] = m[0][0];
       m[0][0] = t;
       t = m[1][0] * ai + m[1][1];
       m[1][1] = m[1][0];
       m[1][0] = t;

       if (realNumber == (double)ai) {
           // AF: division by zero
           break;
       }

       realNumber = 1 / (realNumber - (double)ai);

       if (realNumber > (double)0x7FFFFFFF) {
           // AF: representation failure
           break;
       }
   }

   ai = (maxDenominator - m[1][1]) / m[1][0];
   m[0][0] = m[0][0] * ai + m[0][1];
   m[1][0] = m[1][0] * ai + m[1][1];
   return (Fraction) { .nominator = m[0][0], .denominator = m[1][0], .error = startx - ((double)m[0][0] / (double)m[1][0]) };
}

这样调用它:

double aReal = 123.45;
long maxDenominator = 42;
Fraction aFraction = fractionFromReal(aReal, maxDenominator);
printf("Real %.3f -> fraction => %ld/%ld, error: %.3f\n",
       aReal,
       aFraction.nominator,
       aFraction.denominator,
       aFraction.error);

打印这个:

Real 123.450 -> fraction => 3827/31, error: -0.002

最后但同样重要的是,让我们看看我们如何将新制作的分数放入文本字​​段:

double myVariable = 7.5;
long maxDenominator = 1000; //sample value
Fraction myFraction = fractionFromReal(abs(myVariable - (NSInteger)myVariable), maxDenominator);
[theTextField setText:[NSString stringWithFormat:@"%d %d/%d", (NSInteger)myVariable, myFraction.nominator, myFraction.denominator]];

预期输出:"7 1/2",实际输出:"7 499/999"
有关为什么会发生这种情况的一些信息,请参阅相关问题的答案:How to convert floats to human-readable fractions?

https://stackoverflow.com/questions/5552537/

相关文章:

git - Git 提交消息的推荐格式

c# - 在 C# 中将 Excel 范围作为文本数组或单元格格式的快速方法?

java - 是否有可用的 Java 库完全支持 Excel 的格式字符串?

c++ - 输出带有数字分组的数字(1000000 为 1,000,000 等等)

xml - 选择性地关闭 XML 文件的 Eclipse 格式

visual-studio-2008 - 如何添加法国商标符号 (MD)?

javascript - Highcharts y轴千位分隔符

python - 如何在没有任何额外符号(方括号 [] 和元素之间的空格)的情况下打印 Numpy

c++ - 使用 BOOST_FOREACH 时如何使 Eclipse CDT 自动缩进?

perl - 在 Perl 中获取 UTC 偏移量的最佳方法是什么?