Percentage calculation
(current / maximum) * 100
. In your case, (2 / 10) * 100
.
c# Percentage calculator
Try this
sum = (x * 3.4528) * 1.21;
C# percentage calculation suddenly goes negative
Lasse V. Karlsen gave the correct explanation in the comment.
To fix it, change:
var progress = (int) Math.Round((double) (100 * totalBytesCopied) / sourceLength);
into:
var progress = (int) Math.Round(((double)100) * totalBytesCopied / sourceLength);
or simply:
var progress = (int) Math.Round(100.0 * totalBytesCopied / sourceLength);
As he says, this will force everything to be done as floating point. The integers will automatically (implicitly) be converted to double
in that case. I removed a redundant parenthesis (but you can also just keep it).
Calculate percentage if percentage sign is in the text
You can use this code,
private void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (txtdiscount.Text.Length > 0 && lbltotal.Text != "")
{
decimal net = 0, total = 0, discount = 0;
total = Convert.ToDecimal(lbltotal.Text);
if (txtdiscount.Text.IndexOf('%') != -1)
{
discount = total * Convert.ToDecimal(txtdiscount.Text.Split('%')[0])/100;
}
else
{
discount = Convert.ToDecimal(txtdiscount.Text);
}
net =total- discount;
lblnetamount.Text = net.ToString();
}
}
Calculating the percentage difference between two values
What to do in that situation is subjective, and likely depends on your business needs.
I would prefer Not Available if there is no other choice.
100% may make sense.
Maybe use 1 instead of zero may also make sense resulting in 2560% in your example.
What's the best way to create a percentage value from two integers in C#?
How about just mappedItems * 100.0 / totalItems
and casting this to the appropriate type?
Percentage Based Probability
This is very easy to check for yourself:
Random rand = new Random();
int yes = 0;
const int iterations = 10000000;
for (int i = 0; i < iterations; i++)
{
if (rand.Next(1, 101) <= 25)
{
yes++;
}
}
Console.WriteLine((float)yes/iterations);
the result:
0.2497914
The conslusion: Yes, yes it is.
Edit: Just for fun, the LINQy version:
Random rand = new Random();
const int iterations = 10000000;
int sum = Enumerable.Range(1, iterations)
.Count(i => rand.Next(1, 101) <= 25);
Console.WriteLine(sum / (float)iterations);
Related Topics
Automatically Update Values in Database from Datagridview
Add Header and Footer for Pdf Using Itextsharp
How to Extract Custom Header Value in Web API Message Handler
Unit Test a Method That Returns a Void
How to Add/Update Child Entities When Updating a Parent Entity in Ef
How to Loop Through All Fields in an Object in C#
How to Trim All Elements in a List
Drawing a Triangle With Rounded Corners
Generate C# Class from SQL Server Table Without Store Procedure
Get Properties and Values from Unknown Object
Convert from Httpresponsemessage to Iactionresult in .Net Core
How to Show Alert Message in MVC 4 Controller
Add Spaces Before Capital Letters
C# How to Simply Encrypt a Text File With a Pgp Public Key
Newtonsoft Json Dynamic Property Name