How to round decimal to an int in c# [duplicate] - c#

This question already has answers here:
How might I convert a double to the nearest integer value?
(8 answers)
Closed 9 years ago.
I'am producing decimal such as:
0.8235294117647058823529411765
0.1764705882352941176470588235
I'd like to multiply them by 10 then round them. If the second number after the dot is less than 5 then make it 0. Otherwise, make it 1. For the above examples, that would be:
8
2
The result should be put in a int.

I thought it is simple.
Math.Round(10 * your_decimal);
Reference: http://msdn.microsoft.com/en-us/library/3s2d3xkk.aspx

You can use Math.Round() if you want to keep it as a decimal or round to a certain precision.
decimal dec = 0.8235294117647058823529411765m;
decimal rounded = Math.Round(dec * 10); // 8m
decimal roundedToOne = Math.Round(dec * 10, 1); // 8.2m
FYI, an explicit conversion is defined for decimal to int so you can round down by casting to an int
int a = (int)(dec * 10); // 8
This could be combined with a condition to round the number up and down
decimal num = dec * 10;
int a = (int)num + (num % 0 < .5m ? 0 : 1); // 8

Related

Percentage calculation always returns me 0 [duplicate]

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 4 years ago.
I'm calculating the percentage to add in a datagrid, but when I report the data it always returns me 0, what am I doing wrong?
if I have the following case: the variable quantidadeInstalada has the value of 10 and the goal has 20 the concluido would have to return me 50 but it returns me 0
private void AdicionarPessoa()
{
string Valida = ValidaPessoa();
if (Valida.Equals(""))
{
double concluido=0, falta=0;
int quantidadeInstalada = Convert.ToInt32(ttbQuantidade.Text);
int meta = Convert.ToInt32(ttbMetaPessoa.Text);
concluido = (quantidadeInstalada/meta)*100;
falta = 100-concluido;
MessageBox.Show(concluido.Text);
}
else
MessageBox.Show(Valida);
}
It's probably due to the precision of the int. Use a decimal or double instead.
When we use an integer, we lose precision.
Console.WriteLine(100 / 17); // 5
Console.WriteLine(100 / 17m); // 5.8823529411764705882352941176
Console.WriteLine(100 / 17d); // 5.88235294117647
Console.WriteLine(100 / 17f); // 5.882353
Since integers always round down, 0.99 as an integer is 0.
Note that for precision, the types of the inputs matters.
double output = input1 * input2;
For example:
double outputA = 9 / 10;
Console.WriteLine(outputA); // 0
double outputB = 9 / 10d;
Console.WriteLine(outputB); // 0.9
double outputC = 9d / 10;
Console.WriteLine(outputC); // 0.9
Here is a Fiddle.
It's because you're doing integer division. You can fix it by casting one of the variables to a double like this:
concluido = ((double)quantidadeInstalada/meta)*100;
When you're dividing int by int (as in "quantidadeInstalada/meta"), you'll get an int. Either use a fractional type (e.g. decimal, double) from the very beginning, as Shaun suggested, or (if the integral types have to stay), cast the values to a fractional type in the division expression (as itsme86 shown).

multiplying a decimal by a floating point number [duplicate]

This question already has answers here:
Division returns zero
(8 answers)
Closed 5 years ago.
Im trying to get 75% of the CTotal but as it is a decimal number it is rounding 0.75 to 0, does anyone know of a work around
decimal refundtot = order.CTotal;
//change it as it is making it = 0
refundtot = (75 / 100) * refundtot;
refund.RefundTotal = refundtot;
You should use one decimal number when you dividing numbers.
Your code should look like this :
efundtot = ((decimal)75 / 100) * refundtot;

Convert millisecond to seconds like (0.24) [duplicate]

This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Closed 6 years ago.
I have one integer value which is 48. when i am using divide for this number from 100 using calculator then the value is 0.48. but the same i am trying with my code then getting 0 value.
like int value=48.
int changeValue = 48/100;
changeValue value is:0.
Need help.
int is for integer values. use float or double:
double changeValue = 48.0 / 100;
When you are trying to divide one integer number on another you get also integer value and the value is rounded to lower (24 / 5 = 4)
If you want to get not integer value you should make sure that one of your numbers is also not integer. Thats why I wrote 48.0 intead of 48.
If you alread have integer value in an integer variable, like
int msec = 48;
you should first make it floating:
int msec = 48;
double sec = (double)msec / 100;

Which .net math function always rounds up [duplicate]

This question already has answers here:
How to round up value C# to the nearest integer?
(10 answers)
Closed 7 years ago.
If I have a number, I would like it to output a rounded up number, like this:
1.12 = 2
1.30 = 2
0.89 = 1
Using Math.Round I was only able to get it to round up when it was over half.
Math.Ceiling should do the trick.
Returns the smallest integral value that is greater than or equal to
the specified double-precision floating-point number.
https://msdn.microsoft.com/en-us/library/zx4t0t48%28v=vs.110%29.aspx
static void Main(string[] args)
{
double temp1 = 1.12;
double temp2 = 1.30;
double temp3 = 0.89;
Console.WriteLine(Math.Ceiling(temp1));
//2
Console.WriteLine(Math.Ceiling(temp2));
//2
Console.WriteLine(Math.Ceiling(temp3));
//1
Console.ReadLine();
}
Please use Math.Ceiling instead.
There is Math.Ceiling for that
double dbl = Math.Ceiling(1.1); // 2
Note that
double dbl = Math.Ceiling(-1.1); // 1
Math.Ceiling returns the smallest integral value that is greater than or equal to the specified number.
int num = (int)Math.Ceiling(1.12);
Try using Math.Ceiling.
It will always round up :)
https://msdn.microsoft.com/en-us/library/zx4t0t48%28v=vs.110%29.aspx

C# decimal number round after point rest [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
how to return decimal with long rest after the point like that:
3.786444499963
to this:
3.787
its not just cut the points it also round the rest of the number
Math.Ceiling(3.786444499963 * 1000) / 1000;
But the generally accepted rounding of 3.786444499963 to three decimal places is 3.786. Why do you think otherwise?
Thus:
var round = Math.Round(3.786444499963m, 3, MidpointRounding.AwayFromZero);
Console.WriteLine(round == 3.786m); // prints true
If you want it to ALWAYS round up:
var round = Math.Round(3.786444499963m + 0.0005m, 3);
Console.WriteLine(round == 3.787m); // prints true
Do you see what I did there? I added 0.0005m to the input before using Math.Round. In general, to round x to n decimal places,
var round = Math.Round(x + 5m * Convert.ToDecimal(Math.Pow(10, -n - 1)), n);
Or, perhaps, to avoid the ugly double/decimal conversion:
int k = 1;
decimal value = 5m;
while(k <= n + 1) { value /= 10m; k++; }
var round = Math.Round(x + value, n);
There's an edge case you need to be aware of. What happens to 3.786? Should it be rounded up to 3.787 or remain at 3.786? You haven't specified what you want exactly, so I'll leave this edge case to you.
RoundUp(3.786444499963M, 3);
static decimal RoundUp(decimal dec, int precision)
{
decimal rounder = (decimal)(0.5 * Math.Pow(10, -precision));
return Math.Round(dec + rounder, precision);
}

Categories