简体   繁体   English

在Objective-C中舍入到(1,2或5)x 10 ^ n?

[英]Round to (1, 2, or 5) x 10^n in Objective-C?

Is there a simple way of rounding a value either down to or to the nearest (1, 2, or 5) x 10^n where n is an integer? 是否有一种简单的方法可以将值舍入到最接近的值(1,2或5)x 10 ^ n,其中n是整数? As in one of {..., .05 .1, .2, .5, 1, 2, 5, 10, 20, 50, 100...} 如{...,。05 .1,.2,.5,1,2,5,10,20,50,100 ......}之一

Thanks. 谢谢。

You can take the d=floor(log10(n)) of your number n to get the scale, then divide by 10 to the d to normalize the number to the range of [1.0,10.0) . 你可以取你的数字nd=floor(log10(n))得到比例,然后除以10到d ,将数字标准化到[1.0,10.0)的范围。 From that point it should be easy to round because there are very limited possibilities. 从那时起它应该很容易圆,因为可能性非常有限。 Once you've done that, multiply by 10 to the d to restore the number to the original range. 一旦你做到了这一点,乘以10至d的数量恢复到原来的范围。

The following function is in C, I don't know enough about Objective-C to know if this is idiomatic. 以下函数在C中,我对Objective-C知之甚少,不知道这是不是惯用的。

double RoundTo125(double value)
{
   double magnitude;
   magnitude = floor(log10(value));
   value /= pow(10.0, magnitude);
   if (value < 1.5)
      value = 1.0;
   else if (value < 3.5)
      value = 2.0;
   else if (value < 7.5)
      value = 5.0;
   else
      value = 10.0;
   value *= pow(10.0, magnitude);
   return value;
}

To round x down to the nearest value of c * 10^n , use 要将x向下舍入到最接近的c * 10^n ,请使用

f(c) = c * 10^(floor(log(x/c)))

(assuming x is positive). (假设x为正)。 So to round down to the nearest any of those, just find 所以要向下舍入到最近的任何一个,只需找到

max(f(1), f(2), f(5))

To round x up to the nearest value of c * 10^n , use 要将x舍入到最接近的c * 10^n ,请使用

g(c) = c * 10^(ceiling(log(x/c)))

(again assuming x is positive). (再次假设x为正)。 So to round up to the nearest of any of those, just find 所以要找到最接近的那些,只需找到

min(g(1), g(2), g(5))

Now to simply round to the nearest of any of those values, find the nearest rounding down (first paragraph) and the nearest rounding up (second paragraph), and choose whichever is closer to x . 现在简单地舍入到这些值中最接近的值,找到最近的向下舍入(第一段)和最近的向上舍入(第二段),并选择更接近x那个。

As I see you work with numbers with floating point. 我看到你使用浮点数来处理。 I want to point you that in standard C library there are two functions that can help you: 我想指出,在标准C库中有两个功能可以帮助您:

double floor(double); //round down
double ceil(double);  //round up

They return rounded number. 它们返回舍入的数字。 Also there are another rounding functions. 还有另一个舍入函数。 You can find reference of them here . 你可以在这里找到他们的参考。 After you learn how they work, you may write your own rounding function. 在了解它们的工作原理后,您可以编写自己的舍入函数。 Which will use normalization. 哪个会使用规范化。 Look at example: 看一下例子:

// this function will round to tens
float my_floor(float d)
{
    return floor(d/10)*10;
}

The logarithm approach will work, but I would experiment with string conversion. 对数方法可行,但我会尝试字符串转换。 I don't know Objective C, although Google implies you want something called stringWithFormat . 我不知道Objective C,虽然Google暗示你需要一些名为stringWithFormat东西。 In Java, the code would be 在Java中,代码将是

String s = String.format("%f", theNumber);
StringBuffer buf = new StringBuffer(s);
int len = buf.length()-1;
char c = buf.getCharAt(len);
switch (c)
{
case '9': case '8': case '7': case '6':
buf.setCharAt(len) = '5'; break;
case '4' : case '3':
buf.setCharAt(len) = '2' break;
default: break; /* don't change '0', '1', '2', '5' */
}
double roundNumber = Double.parseDouble(buf.toString());

Might be faster. 可能会更快。 (Might be slower!) You could also try searching the string for the decimal point and subtracting an increment from the original number once you new the magnitude of the last decimal place. (可能会慢一些!)您也可以尝试在字符串中搜索小数点,并在新增最后一个小数位的大小后从原始数字中减去增量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM