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 `

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.

Use Ceiling

``````math.ceiling (1.0 / 30.0)
``````

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

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

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

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.