hi,

how to round up a number.

e.g. 3.4 to 3 and 3.5 to 4

thanx in advance

Jump to Posthi,

how to round up a number.

e.g. 3.4 to 3 and 3.5 to 4thanx in advance

Use:

`Math.Round(4.4));`

Jump to PostThere is a quirk with Math.Round. It defaults to rounding to the nearest even number, so 1.5 = 2, 2.5 = 2. If you use

`Math.Round(2.5, MidpointRounding.AwayFromZero)`

it will round to the nearest whole number.

Jump to Poste.g. 3.4 to 3

Math.Ceiling(ValueToRoundup)

Except calling Math.Ceiling() on 3.4 will give you 4.0 not 3.0.

RunTimeError
0
Junior Poster in Training

hi,

how to round up a number.

e.g. 3.4 to 3 and 3.5 to 4thanx in advance

Use:

`Math.Round(4.4));`

Geekitygeek
480
Nearly a Posting Virtuoso

`Math.Round(2.5, MidpointRounding.AwayFromZero)`

it will round to the nearest whole number.

jwcochrane
0
Newbie Poster

us the math library in C#

Math.Ceiling(ValueToRoundup)

hi,

how to round up a number.

e.g. 3.4 to 3 and 3.5 to 4thanx in advance

Edited
by jwcochrane because:
*
n/a *

jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster

e.g. 3.4 to 3

Math.Ceiling(ValueToRoundup)

Except calling Math.Ceiling() on 3.4 will give you 4.0 not 3.0.

Edited
by jonsca because:
*
n/a *

jwcochrane
0
Newbie Poster

Ok just realised he was not asking for a RoundUp function. just basic rounding.

Ceiling = same as RoundUp (to int)

Floor = same as RoundDown (to int)

Round = rounds to the nearest decimal part. (What he is looking for)

Only issue with Ceiling is it returns an integer.

Except calling Math.Ceiling() on 3.4 will give you 4.0 not 3.0.

Edited
by jwcochrane because:
*
n/a *

jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster

Ok just realised he was not asking for a RoundUp function

It wasn't really your fault the language used was ambiguous.

Only issue with Ceiling is it returns an integer.

You are correct.

I think the OP has lost interest. Hehe.

Geekitygeek
480
Nearly a Posting Virtuoso

Round = rounds to the nearest decimal part. (What he is looking for)

Be careful with Math.Round. See my post above to avoid unexpected results.

ddanbe
2,724
Professional Procrastinator
Featured Poster

SharpJohnnyG
0
Newbie Poster

This depends on the type but you would need something like

```
Decimal decTmp = 3.5m;
Decimal decNewTmp = Decimal.Parse(decTmp, 0);
```

with the 0 being the number of decimal places you would like to round to

hi,

how to round up a number.

e.g. 3.4 to 3 and 3.5 to 4thanx in advance

Geekitygeek
480
Nearly a Posting Virtuoso

This depends on the type but you would need something like

`Decimal decTmp = 3.5m; Decimal decNewTmp = Decimal.Parse(decTmp, 0);`

with the 0 being the number of decimal places you would like to round to

Have you checked this? None of the overloads for .Parse accept the input types you have shown. It accepts a string and optionally an IFormatProvider and/or a NumberStyle.

Also, people keep pointing to Math.Round() but if you read the article ddanbe linked to you will see that there are several commonly used 'tie breaker' situations when rounding .5 values. The default in visual studio is to Round to the Nearest **Even** number. This is the tiebreaker used in bookkeeping and gives the most accurate summing of values.

It isn't, however, the method most people use. You can specify overload the Math.Round method to specify the MidpointRounding method to use. That way you can be sure of the results your rounding will yield.

Dont mean to go on, but it can trip you up if you dont know its there ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.