Round any n-digit number to (n-1) zero-digits?

I think you should try with something like this.

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. 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.

Much like the previous answer...

Sz by convention is used as prefix for strings in Hungarian notation. – Hasan Khan Jan 5 at 6:08 Thanks! Saved me a nice bit of time and agony :) – bretddog Jan 5 at 6:52 This only works for positive integers.It would be sensible to add a guard clause If number Length like logicnp's answer – MarkJ Jan 5 at 9:44 This won't do proper rounding.

Number / sz is int / int, which will be an integer divide, which does truncation, not rounding. The Math. Round and cast back to int doesn't do anything.

– David Yaw Jan 5 at 15:26 I tried the log version in other answer, but it gave 0 as result from some 3 digit numbers. So looked like an error, but I did not have time to find out why. – bretddog Jan 5 at 17:48.

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.

One of the way could be Convert the number to Decimal Divide it by 10^(n-1) (where n is number of digits) Now use round function (Decimal. Round) Multiply again by 10^(n-1).

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).

Divide the number by 10n and round the result, then multiply the result back with 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.

Int MakeOneSigFig(int value) { int neg = 1; if(value = -10) { return value; } if(value == int. MinValue) { value = int. MaxValue; neg = -1; } if(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.

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. That would be more efficient, but more code and not quite as easy to read.

If(value.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions