Round a floating point number to next coming number - c#

I want to round off a decimal number to the next higher number nn c#.
for example:
23.3 should become 24
25.8 should become 26
26.1 should become 27
currently i am using this code but it is not fulfilling my requirements.
double pages = Math.Floor((float)anyNumber / 5);

Math.Floor rounds down, use Math.Ceiling to round up:
double pages = Math.Ceiling( anyNumber / 5.0 );

Math.Ceiling will do what you want...
double pages = Math.Ceiling((float)anyNumber / 5);
as an aside, you might choose to cast to double instead of float to avoid extra implicit casts from float to double:
double pages = Math.Ceiling((double)anyNumber / 5d);

Related

C# round float to the next quarter (0.25) [duplicate]

Is there a simple way in c# to round a decimal to the nearest quarter i.e. x.0, x.25, x.50 x.75
for example 0.21 would round to 0.25, 5.03 would round to 5.0
Thanks in advance for any help.
Multiply it by four, round it as you need to an integer, then divide it by four again:
x = Math.Round (x * 4, MidpointRounding.ToEven) / 4;
The various options for rounding, and their explanations, can be found in this excellent answer here :-)
Alternatively, you can use UltimateRoundingFunction given in this blog:
http://rajputyh.blogspot.in/2014/09/the-ultimate-rounding-function.html
//amountToRound => input amount
//nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1
//fairness => btween 0 to 0.9999999___.
// 0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling
// 0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1.
// 0.4999999... non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2.
// 0.75 means first 75% values will be rounded down, rest 25% value will be rounded up.
decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
{
return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;
}
Call below for standard rounding. i.e. 1.125 will be rounded to 1.25
UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);
Call below for rounding down border values. i.e. 1.125 will be rounded to 1.00
UltimateRoundingFunction(amountToRound, 0.25m, 0.4999999999999999m);
So called "Banker's Rounding" is not possible with UltimateRoundingFunction, you have to go with paxdiablo's answer for that support :)
An extension method based on this answer.
namespace j
{
public static class MathHelpers
{
public static decimal RoundToNearestQuarter(this decimal x)
{
return Math.Round(x * 4, MidpointRounding.ToEven) / 4;
}
}
}

Rounding up to 2 decimal places in C#

I have a decimal number which can be like the following:
189.182
I want to round this up to 2 decimal places, so the output would be the following:
189.19
Is there built in functionality for this in the Math class, or something else? I know the ceiling function exists but this doesn't seem to do what I want - it'll round to the nearest int, so just '189' in this case.
Multiply by 100, call ceiling, divide by 100 does what I think you are asking for
public static double RoundUp(double input, int places)
{
double multiplier = Math.Pow(10, Convert.ToDouble(places));
return Math.Ceiling(input * multiplier) / multiplier;
}
Usage would look like:
RoundUp(189.182, 2);
This works by shifting the decimal point right 2 places (so it is to the right of the last 8) then performing the ceiling operation, then shifting the decimal point back to its original position.
You can use:
decimal n = 189.182M;
n = System.Math.Ceiling (n * 100) / 100;
An explanation of the various rounding functions can be found here.
Be aware that formulae like this are still constrained by the limited precision of the double type, should that be the type you are using (your question stated decimal but it's possible you may just have meant a floating point value with fractional component rather than that specific type).
For example:
double n = 283.79;
n = System.Math.Ceiling (n * 100);
will actually give you 28380, not the 283.79 you would expect(a).
If you want accuarate results across the board, you should definitely be using the decimal type.
(a) This is because the most accurate IEEE754 double precision representation of 283.79 is actually:
283.790000000000020463630789891
That extra (admittedly minuscule) fractional component beyond the .79 gets ceilinged up, meaning it will give you a value higher than you would expect.
var numberToBeRound1 = 4.125;
var numberToBeRound2 = 4.175;
var numberToBeRound3 = 4.631;
var numberToBeRound4 = 4.638;
var numberOfDecimalPlaces = 2;
var multiplier = Math.Pow(10, numberOfDecimalPlaces);
//To Round Up => 4.13
var roundedUpNumber = Math.Ceiling(numberToBeRound1 * multiplier) / multiplier;
//To Round Down => 4.12
var roundedDownNumber = Math.Floor(numberToBeRound1 * multiplier) / multiplier;
//To Round To Even => 4.12
var roundedDownToEvenNumber = Math.Round(numberToBeRound1, numberOfDecimalPlaces, MidpointRounding.ToEven);
//To Round To Even => 4.18
var roundedUpToEvenNumber = Math.Round(numberToBeRound2, numberOfDecimalPlaces, MidpointRounding.ToEven);
//To Round To Away From Zero => 4.63
var roundedDownToAwayFromZero = Math.Round(numberToBeRound3, numberOfDecimalPlaces, MidpointRounding.AwayFromZero);
//To Round To Away From Zero => 4.64
var roundedUpToAwayFromZero2 = Math.Round(numberToBeRound4, numberOfDecimalPlaces, MidpointRounding.AwayFromZero);
How about
0.01 * ceil(100 * 189.182)
In .NET Core 3.0 and later versions, three additional rounding strategies are available through the MidpointRounding enumeration.
Besides MidpointRounding.AwayFromZero and MidpointRounding.ToEven it now includes:
1. MidpointRounding.ToNegativeInfinity
2. MidpointRounding.ToPositiveInfinity
3. MidpointRounding.ToZero
For this specific question you need to use MidpointRounding.ToPositiveInfinity, this will round the number up always.
Note this only works if the number isn't negative. See table below for examples.
Original number
ToNegativeInfinity
ToPositiveInfinity
ToZero
3.55
3.5
3.6
3.5
2.83
2.8
2.9
2.8
2.54
2.5
2.6
2.5
2.16
2.1
2.2
2.1
-2.16
-2.2
-2.1
-2.1
-2.54
-2.6
-2.5
-2.5
-2.83
-2.9
-2.8
-2.8
-3.55
-3.6
-3.5
-3.5
For more information about midpointrounding see https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding
And of course the code to make it work:
// function explained: Math.Round(number, amount of decimals, MidpointRounding);
decimal number = 189.182m;
number = Math.Round(number, 2, MidpointRounding.ToPositiveInfinity);
// result: number = 189.19
public static decimal RoundUp(decimal input, int places)
{
decimal multiplier = (decimal)Math.Pow(10, places);
return decimal.Ceiling(input * multiplier) / multiplier;
}
// Double will return the wrong value for some cases. eg: 160.80
public static decimal RoundUp(decimal input, int places)
{
decimal multiplier = Convert.ToDecimal(Math.Pow(10, Convert.ToDouble(places)));
return Math.Ceiling(input * multiplier) / multiplier;
}
One other quirky but fun way to do it is Math.Round() after offsetting the number.
decimal RoundUp(decimal n, int decimals)
{
n += decimal.Parse($"1e-{decimals}", System.Globalization.NumberStyles.AllowExponent) / 2;
n -= 1e-28m;
return Math.Round(n, decimals);
}
decimal RoundDown(decimal n, int decimals)
{
n -= decimal.Parse($"1e-{decimals}", System.Globalization.NumberStyles.AllowExponent) / 2;
n += 1e-28m;
return Math.Round(n, decimals);
}
Is has the advantage of not using Math.Pow() which uses double and thus can cause unpredictable rounding errors.
This solution basically uses the fact that midpoint rounding can be turned into up/down rounding if you increase/decrease the number a little:
Math.Round(3.04m, 1) is 3.0 - not what we want
Let's add 0.04(9) to it
Math.Round(3.0899999999999999999999999999m, 1) is 3.1 - success!
Subtracting 1e-28m (= 0.0000000000000000000000000001) is important, because we want to be able to round up 3.0000000000000000000000000001 to 4, but 3.0000000000000000000000000000 should stay 3.

Custom Rounding of decimal type in C#

Guys,
I am writing a method for rounding. Input is a decimal type (four decimal places guaranteed). The rounding rule is that 0.005 or less is ignored, i.e. look at third decimal place - if it is <= 5, round down else round up.
Some use cases : 82.3657 -> 82.36, 82.3667 -> 82.37, 82.5967 -> 82.60, 82.9958 -> 82.99, 82.9968 -> 83.00
Any good ideas? I have worked it out as follows.
private decimal CustomRound(decimal x)
{
decimal rX = Math.Truncate(x * 100) / 100;
decimal x3DecPlaces = Math.Truncate(x * 1000) / 1000;
decimal t = (x3DecPlaces * 1000) % 10;
if (t >= 6)
rX = rX + 0.01m;
return rX;
}
I don't believe there's anything built-in for that, because it's a pretty unusual requirement (for example the idea that 1.3358 is closer to 1.33 than to 1.34 is odd). Your code looks reasonably appropriate.
EDIT: You can't use MidpointRounding to get the effect you want here, because the point at which you start rounding up isn't the midpoint - it's (say) 1.336 rather than the normal 1.335. Only 1.335 is treated as the midpoint between 1.33 and 1.34, because that is the mid-point. You've effectively got a biased rounding here in an unusual way.
You can't even just truncate to three DP and then use MidpointRounding, as there's no "towards zero" mode.
One slightly odd option would be to effectively perform the bias yourself:
private static decimal CustomRound(decimal x)
{
return decimal.Round(x - 0.001m, 2, MidpointRounding.AwayFromZero);
}
So it would treat 82.3657 as 82.3647 and round that to 82.36; it would treat 82.3667 and 82.3657 and round it to 82.37, and it would treat 82.5967 as 82.5957 and round it to 82.60 etc. I think that does what you want - but only for positive values. You'd need to work out exactly what behaviour you want for negative values.
Whatever you do, you need to document it very clearly :)
Just as a matter of preference, I would use decimal.Truncate rather than Math.Truncate, just to make it clearer that everything really is done with decimals.
How do you want to handle negative values? I suppose you would want -13.999 to round to -14 not to -13.99 right?
In that case your +/- 0.01m should depend on whether x is negative or positive.
This is an easier way to do it:
decimal CustomRound(decimal x)
{
var offset = x >= 0 ? -0.001m : 0.001m;
return Decimal.Round(x + offset, 2, MidpointRounding.AwayFromZero);
}
Maybe You could use Math.Round(Double, Int32) Method?

How can I round numbers up instead of down?

I'm performing some calculations and inserting the result into a database.
My problem is, that the answers I'm getting seem to be rounding down rather than up. This might not seem important but over the course of a lot of sales, the cents start adding up!!
Decimal pubCut = rrp * (percentageCutD / 100);
Decimal retCut = rrp * retailerCut;
Decimal edcut = rrp * edpercentage;
I'll be honest, I'm rubbish with figures and the whole Maths function was something I tried to avoid in college. Can anyone tell me how I can get these figures to round up as opposed to down?
Use Math.Ceiling() method.
double[] values = {7.03, 7.64, 0.12, -0.12, -7.1, -7.6};
Console.WriteLine(" Value Ceiling Floor\n");
foreach (double value in values)
Console.WriteLine("{0,7} {1,16} {2,14}",
value, Math.Ceiling(value), Math.Floor(value));
// The example displays the following output to the console:
// Value Ceiling Floor
//
// 7.03 8 7
// 7.64 8 7
// 0.12 1 0
// -0.12 0 -1
// -7.1 -7 -8
// -7.6 -7 -8
Your problem is this
(percentageCutD / 100)
Since 100 is an int, it will perform integer division, so that 150/100 becomes 1. You can fix this by maksing sure that 100 is a decimal since you want a decimal as result in the end. Change your code to.
(percentageCutD / 100D)
However, if you always want to round values even like 1.1 up to 2, then you will have to use Math.Ceiling to accomplish this. If you for some reason want to avoid the Math class (I can't see why you want to do it, you can add 1 to the result and cast it to an int to effectively round up to the nearest integer.
.Net's Math.Round function uses something commonly referred to as banker's rounding which works by rounding .5 to the nearest even integer, ie 22.5 = 22 and 23.5 = 24. This gives a more even distribution when rounding.
It's also worth noting the SQL server doesn't use bankers rounding
Your incoming percentages probably don't add to 100 %. Compute one of your fractions by substracting the sum of the other fractions from the total.
var v = 100D;
var p1 = 33D/100;
var p2 = 33D/100;
var p3 = 33D/100;
var v1 = p1*v;
var v2 = p2*v;
var v3 = p3*v;
var sum = v1 + v2 + v3;
Unfortunately sum is 99 and not 100.
You can compute v3 like this instead:
var v3 = v - (v1 + v2);
Or better fix the rounding error in the incoming percentages:

Round a decimal to the nearest quarter in C#

Is there a simple way in c# to round a decimal to the nearest quarter i.e. x.0, x.25, x.50 x.75
for example 0.21 would round to 0.25, 5.03 would round to 5.0
Thanks in advance for any help.
Multiply it by four, round it as you need to an integer, then divide it by four again:
x = Math.Round (x * 4, MidpointRounding.ToEven) / 4;
The various options for rounding, and their explanations, can be found in this excellent answer here :-)
Alternatively, you can use UltimateRoundingFunction given in this blog:
http://rajputyh.blogspot.in/2014/09/the-ultimate-rounding-function.html
//amountToRound => input amount
//nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1
//fairness => btween 0 to 0.9999999___.
// 0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling
// 0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1.
// 0.4999999... non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2.
// 0.75 means first 75% values will be rounded down, rest 25% value will be rounded up.
decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
{
return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;
}
Call below for standard rounding. i.e. 1.125 will be rounded to 1.25
UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);
Call below for rounding down border values. i.e. 1.125 will be rounded to 1.00
UltimateRoundingFunction(amountToRound, 0.25m, 0.4999999999999999m);
So called "Banker's Rounding" is not possible with UltimateRoundingFunction, you have to go with paxdiablo's answer for that support :)
An extension method based on this answer.
namespace j
{
public static class MathHelpers
{
public static decimal RoundToNearestQuarter(this decimal x)
{
return Math.Round(x * 4, MidpointRounding.ToEven) / 4;
}
}
}

Categories