This question already has answers here:
C# decimal take ceiling 2
(5 answers)
Closed 7 years ago.
How would I round up the amount of 3.79 to 3.80?
double number = Math.Ceiling(3.79); //Gives me 4, but I need 3.80
It's a simple question, but I do not know how to achieve it. Thank you in advance.
How about using Math.Round(double, int) overload with 1 digit?
Math.Round(3.79, 1) // 3.8
Are you looking for
double number = Math.Ceiling(3.79 * 10.0) / 10.0;
if you want to Round (not find the Ceiling)
double number = Math.Round(3.79, 1);
Please, note the difference:
Ceiling(32.1) == 33
Round(32.1) == 32
Related
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:
How do I round to the nearest 0.5?
(10 answers)
Closed 5 years ago.
My data is 10.17 , i want get 10.50 after round .
This code that i used totQty = Math.Round(totQty, 1, MidpointRounding.ToEven) but get 10.20.
To do this, you can multiply your number by 2, round up with 0 decimals, and then divide that by 2.
double rounded = Math.Ceiling(2 * value) / 2;
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
This question already has answers here:
C# simple divide problem
(9 answers)
Closed 7 years ago.
This mabey is a dumb question but i try to count discount on a price like this:
newAllaVaror.Pris = System.Convert.ToDouble(
(1 - (clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)) * vara.Pris
).ToString();
It will look like this in reality (1-(20/100)*7.99), but my output is now 7.99 but it should be 6,392..becuse the orginal price is 7.99...
I have tried to move the parentheses but i only get 1 then..
In wich order does C# go thru the parentheses, becuse this should work right??
I'm pretty sure you are doing an integer division here:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)
So if you have 20/100, it will result in 0 instead of the expected 0.2 as the reminder is truncated.
You need to convert to double, one of the operands:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0)
Since #Scott pointed out your other variables are of type decimal, you need to convert one of the operands to that type instead:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0m)
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