简体   繁体   English

将任何n位数字舍入为(n-1)个零位数

[英]Round any n-digit number to (n-1) zero-digits

Sorry hard to formulate. 很抱歉很难制定。

I need to round like this: 我需要像这样:

12 -> 10
152 -> 200
1538 -> 2000
25000 -> 30000
etc. 

Twisting my head, but can't see how to make this. 扭曲我的头,但看不出如何做到这一点。 Must work for any n number of digits. 必须适用于任意n位数。 Anyone got an elegant method for it? 有人有一个优雅的方法吗?

c# or vb.net c#或vb.net

How about this: 这个怎么样:

        double num = 152;

        int pow = (int)Math.Log10(num);

        int factor = (int)Math.Pow(10, pow);

        double temp = num / factor;

        double result = Math.Round(temp) * factor;

I think you should try with something like this: 我想你应该尝试这样的事情:

public int Round( int number)
{
    int power = number.ToString().Length - 1;
    int sz = Math.Pow(10, power);

    int rounded = (int)Math.Round( number / sz );

    return rounded * sz;
}

The idea is to get the size of the nearest 10 power, available by the length of the number expressed as a string. 我们的想法是获得最接近的10次幂的大小,可以用表示为字符串的数字长度来获得。 Then divide the number by that power, leaving it like 1,2 and then round it using the Math.Round method and restore the size by remultiplying it to the power. 然后将该数除以该幂,将其保留为1,2,然后使用Math.Round方法将其舍入,并通过将其重新乘以幂来恢复大小。

Much like the previous answer... 很像以前的答案......

I would do it this way: 我会这样做:

double d = 25000;
int power = d.ToString().Length - 1;
double multipler = Math.Pow(10,power);
d = Math.Round(d / multipler) * multipler;
Console.WriteLine(d);

One of the way could be 其中一种方式可能是

  1. Convert the number to Decimal 将数字转换为十进制
  2. Divide it by 10^(n-1) (where n is number of digits) 除以10 ^(n-1)(其中n是位数)
  3. Now use round function ( Decimal.Round ) 现在使用圆函数( Decimal.Round
  4. Multiply again by 10^(n-1) 再乘以10 ^(n-1)
int MakeOneSigFig(int value)
{
    int neg = 1;
    if(value <= 10 && value >= -10) { return value; }
    if(value == int.MinValue) { value = int.MaxValue; neg = -1; }
    if(value < 0) { value = -value; neg = -1; }

    int mult = 10; // start at 10 because we've got 'firstDigit = value / 10' below
    while(value > 99) { value /= 10; mult *= 10; }
    int firstDigit = value / 10;
    if(value % 10 >= 5) firstDigit++;
    return neg * firstDigit * mult;
}

This is equivalent to MidpointRounding.AwayFromZero . 这相当于MidpointRounding.AwayFromZero This method doesn't do any double math or string conversions. 此方法不进行任何双重数学或字符串转换。 If you didn't want to loop, you could replace that with the if block below. 如果您不想循环,可以使用下面的if块替换它。 That would be more efficient, but more code and not quite as easy to read. 这将更有效,但更多的代码,而不是那么容易阅读。

if(value < 100) { mult = 10; }
else if(value < 1000) { mult = 100; value /= 10; }
else if(value < 10000) { mult = 1000; value /= 100; }
else if(value < 100000) { mult = 10000; value /= 1000; }
// etc.

Divide the number by 10n and round the result, then multiply the result back with 10n; 将数字除以10n并舍入结果,然后将结果乘以10n;

int MyRound(int num)
{
    double divisor = Math.Pow(10, num.ToString().Length - 1);
    return (int)(Math.Round(num / divisor, MidpointRounding.AwayFromZero) * divisor);
}

Note that we should use MidpointRounding.AwayFromZero when rounding because of the default banker's rounding. 请注意,由于默认银行家的舍入,我们应该在舍MidpointRounding.AwayFromZero使用MidpointRounding.AwayFromZero

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

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