Home c# math.round: round number up to 2 characters

math.round: round number up to 2 characters

Author

Date

Category

We have a number of 4 characters accuracy:

decimal testdecimal = 14.7848m;

And we want to round it up to two characters as we were taught in school, right to left. As a result, you should get 14.79:

console.writeline (testdecimal);
Console.WriteLine (Math.round (TestDecimal, 2, Midpointrounding.AwayFromZero)) // 14.78;

But reality refuses to coincide with school knowledge, and start it under these school knowledge customized:

console.writeline (Math.round (Testdecimal, 3, midpointrounding.awayfromzero)) // 14,785;
Console.WriteLine (Math.round (Math.round (Testdecimal, 3, Midpointrounding.awayFromzero), 2, midpointrounding.awayfromzero)) // 14,79;

Answer 1, Authority 100%

Do someone lame schoolpage?

There is a rounding down (Math.floor () function), rounding up (Math.ceiling () function) and simply rounding to the nearest whole (Math.round () function).

In this case, rounding to the nearest whole: if the final number is less than 5, then rounding down, if more than or equal then up. Since we have a number 14.78 4 8 and the third digit less than 5, then rounded down. The right answer is 14.78.


Answer 2

What is the question?

If rounded 14,7848 to a number with two decimal places, it turns out 14.78. No options.


Answer 3

And why such difficulties? If you need to round the number up , then you can do as for example:

Public Static Decimal Roundup (Decimal Number, Int Digits)
{
  var factor = convert.todecimal (Math.pow (10, Digits));
  RETURN MATH.CEILING (Number * Factor) / Factor;
}
Console.WriteLine (Roundup (14.7848m, 2)); // 14.79
Console.WriteLine (Roundup (14.78424m, 3)); // 14.785
Console.WriteLine (Roundup (14.12m, 1)); // 14.2.

demo: dotnetfiddle

In general, look at the answers from other users. Your judgments about rounding numbers are somewhat strange.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions