hi,
how to round up a number.
e.g. 3.4 to 3 and 3.5 to 4
thanx in advance
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));
There 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.
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
e.g. 3.4 to 3
Math.Ceiling(ValueToRoundup)
Except calling Math.Ceiling() on 3.4 will give you 4.0 not 3.0.
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.
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.
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.
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
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 ;)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.