This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
round up value C#
I want to approximate percentage in ASP.net, for example I had a percentage 72.72727272727% I want it to be approximated to 73%, how can I do that in ASP.net
lnPercentageRnd = Math.Round(72.72727272727, 0, MidpointRounding.AwayFromZero);
The AwayFromZero is important for natural rounding, because .Net defaults to Banker's Rounding.
It's not specific to ASP.NET, but .NET in general.
double d = .7272727272727;
string percent = d.ToString("p0"); // 73%
Percentage = Math.Round(Percentage)
Related
This question already has answers here:
How do I make it round off instead of always round down?
(2 answers)
How to round a decimal up?
(6 answers)
Closed 5 months ago.
This is my code:
decimal test1 = 190.5m;
decimal test2 = 191.5m;
var testResult1 = Math.Round(test1, 0); // =190 It should be 191!!!!
var testResult2 = Math.Round(test2, 0); // =192 Correct.
Is there a way I could get the correct result? Maybe another library instead of System.Math?
This is by design. Albeit not what we have grown up with.
Find how you manage to round the way you expect here:
https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding?redirectedfrom=MSDN&view=net-6.0
For a bit more reading and background, have a browse through some of the answers here:
Why does .NET use banker's rounding as default?
This question already has answers here:
Rounding a variable to two decimal places C# [duplicate]
(8 answers)
How do you round a number to two decimal places in C#?
(15 answers)
Closed 5 years ago.
I have a decimal number decimal n = 0.111111111m; and I want to change it to 0.112. How could I do this ?
You can use:
Math.Round(n, 3);
To always round up, you can use:
Math.Ceiling(n * 1000) / 1000;
According to this link
https://msdn.microsoft.com/en-us/library/6be1edhb(v=vs.110).aspx
You'll need to do something like Decimal.Round(n, 3);
However your initial approximation is not accurate
This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
decimal vs double! - Which one should I use and when? [duplicate]
(7 answers)
Closed 6 years ago.
double sth = 250 - 249.99;
Console.WriteLine(sth);
Why does this return sth like 0.009994507, instead of 0.01?
Floating point numbers (in this case doubles) cannot represent decimal values exactly. For more info, see this page here
If you need a more accurate representation, use decimal instead.
because when you print the double you print the all double value not just the first x after point digits.
you can use String.Format to print only the first 2 numbers.
double sth = 250.00d - 249.99d;
string sthString = String.Format("{0:0.00}", sth);
Console.WriteLine(sthString);
There are a lot of decimals that have infinite binary representation. What you're experiencing is exactly this case.
For more on this topic see: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/
This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.Net Round Bug
In C#: Math.Round(2.5) result is 2 (instead of 3)! Are you kidding me?
Code:
var d1 = Math.Round(187.5); // 188
var d2 = Math.Round(62.5); // 62
Why is it so?
By default, Math.Round uses a form of rounding called Banker's Rounding, which rounds to the nearest even integer when the input is halfway between two integers.
See Why does .NET use banker's rounding as default? for an understanding of this design decision.
If you don't like this behaviour, you can always use this overload of Math.Round, which lets you specify the MidPointRoundingMode (ToEven, AwayFromZero).
You can change this behaviour with an call to this overload of the method - http://msdn.microsoft.com/en-us/library/ms131274.aspx