Home c# how to round the number in the biggest

how to round the number in the biggest

Author

Date

Category

There is an expression

1/30

How to round the result to an integer? For example, in order for the number 1


Answer 1, Authority 100%

For example,

math.ceiling (1.0 / 30.0)

Do not forget that you are using an integer division. The result of division 1/30 is already 0. Therefore, go to the division of fractional numbers (1.0 / 30.0 = 0.03333333 …), and this number is rounded up.


Answer 2, Authority 64%

Use Ceiling

math.ceiling (1.0 / 30.0)

Answer 3, Authority 55%

You can without Math: (A + B - 1) / B


Answer 4, Authority 45%

Option with integer division looks like this:
(A * B + B * B-1) / (B * B)

We will find an integer option.
It is easy to verify that Ceil (A / B) = - Floor (-A / B)
Those. If A / B is rounding down, then - (- A / B) This rounding up.

If with integer division, the fractional part is simply discarded (which corresponds to the operation TRUNC), then in the case of AB & GT; 0 rounding up is - (- A / B) ,
And in the case of AB & LT; 0 is A / B .


Answer 5

From the point of view of setting the problem, then yes, option

math.ceiling (1.0 / 30.0);

will be true.

But from the point of view of logic it would be better to use

(int) math.round (1.0 / 30.0, 0);

rounding occurs in the nearest side

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