C# How to split advanced? [duplicate] - c#

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 6 years ago.
I need to split a number with decimal places.
An example: I need to do 14525/1024/1024 but in my application, it says
0 MB
How to calculate 14525/1024/1024 to
00,01 MB
like
40.61 MB
(I need to convert bytes to megabytes)

You need to use decimal and not int:
decimal result = Math.Round((decimal)14525 / 1024 / 1024, 2);

cast any value to Double before to do the division

Related

Is there a way to display 2 decimal places instead of automatically rounding? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 3 years ago.
I'm making a simple calculator in c# and and everything works well until I try dividing numbers that should give decimal places. How to display 2 decimal places in these cases?
I've tried putting #.## after .ToString.
{label1.Text = (divide / Convert.ToInt64(label1.Text)).ToString("#.##");}
I expect the output of 5/4 to be 1.25, but it is 1.
convert the number to float instead of int,
{label1.Text = ((float)divide / float.Parse(label1.Text)).ToString("n2");}

How to round a decimal number? [duplicate]

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

why does double return this value? C# [duplicate]

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/

taking just two digits of a Number of type double without rounding it [duplicate]

This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 8 years ago.
I have this Number
double Nb=4.9584763251
how can I make get only two digits after the comma without rounding the number :
Nb=`4.95` and not `4.96`
using the Math.Round(Nb,2) will return 4.96
is there any way to fix this ?
You may use Math.Truncate(Nb * 100) / 100m
double x = ((int)(Nb*100))/100.0

Divide not returning the decimal value I expect [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's wrong with this division?
If you divide 2 / 3, it should return 0.66666666666666667. Instead, I get 0.0 in double value type and 0 in decimal.
My purpose is to divide even (e.g. 2 / 3) and round to 1 always to the nearest.
Any help?
You're doing integer division, from the sounds of it. Try this:
decimal result = 2.0 / 3.0;
Or even force it to decimals for all of the operations:
decimal result = 2.0m / 3.0m;
This should give you a result more like you expect.
Doing 2/3 is integer division which will not return the decimal place of the division. To get .666666667 you will need to do 2.0 / 3.0 which are both doubles to get the expected answer.

Categories