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;
Related
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 2 years ago.
I have the amount
var Amount = 23454;
And I need to Format it like this: 234.54
I searched for similar problems and tried this:
String.Format("{0:0.00}", Amount / 100) // 234.00
String.Format("{0:0.##}", Amount / 100) // 234
(Amount / 100).ToString(CultureInfo.CreateSpecificCulture("en-GB")) // 234
but it removes decimals
This should do the trick:
String.Format("{0:0.##}", ((Decimal)Amount) / 100)
As mentioned before, you are working with an integer and not a decimal. You have to cast it before doing your logic
This question already has answers here:
Why is modulus operator not working for double in c#?
(6 answers)
Closed 4 years ago.
Hi I'm having this really weird issue with C#.
I'm trying to do a modulu operation:
double stepSize = 3.6;
if (180.0 % stepSize != 0) DoStuff();
And 180.0 % 3.6 = 0 -> "proof"
But for some reason C# returns: 3.6
Do any one have an explanation for that
The problem is the floating-point arithmetics and its inability to represent all decimal numbers precisely. A simple test reveals the differences:
decimal type:
Console.WriteLine(180m % 3.6m); // 0.0
float type:
Console.WriteLine(180f % 3.6f); // 4.768372E-06
double type:
Console.WriteLine(180d % 3.6d); // 3.6
This question already has answers here:
Round up to next quarter
(3 answers)
Closed 5 years ago.
Any way to round up decimal value to its next quarter (0.25,0.50,0.75) in C#.Net?
I am getting wrong rounding with Math.Floor function
Example:
5.125 -> 5.25
6.390 -> 6.50
7.610 -> 7.75
8.950 -> 9.00
decimal UltimateRounding(decimal amountToRound, decimal nearstOf, decimal
fairness, decimal final)
{
return Math.Floor(amountToRound / nearstOf + fairness + final) * nearstOf;
}
Try
var a = 5.125m;
a = Math.Ceiling(a * 4) / 4;
This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 5 years ago.
I want to limit decimal place till 4 for below code, how to do that,
var a = Convert.ToDecimal( 80794992640) / (1024 * 1024);
I guess you can round it if you want to fix it to 4 digits:
var a = Math.Round(Convert.ToDecimal( 80794992640) / (1024 * 1024),4 );
but if your concern is to restrict it in the display then you can just apply the restriction in the ToString method:
a.ToString("0.####");
the latter method will keep the precision for the calculations but cut the precision only for display
This will round it to 4 decimal places:
var a = Math.Round(Convert.ToDecimal( 80794992640) / (1024 * 1024), 4);
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