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
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:
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;
This question already has answers here:
Comparing double values in C#
(18 answers)
Closed 8 years ago.
I have this code:
static void Main()
{
float i;
for (i = 0; i <= 100; i = i + 0.01F)
{
Console.WriteLine(i);
}
}
And it prints me this:
0
0.01
0.02
0.03
0.04
0.05
0.05999999
0.06999999
0.07999999
0.08999999
0.09999999
0.11
Is this a bug or am I doing something wrong?
Is there any solution to this?
It is not a bug. This is a fundamental property of floating point numbers. The numbers are stored as a mantissa and an exponent and they cannot represent every number possible with perfect precision. If you need precision, you have to use decimal types.
For a lot of information about this see What Every Computer Scientist Should Know About Floating-Point Arithmetic
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
This question already has answers here:
How do I round to the nearest 0.5?
(10 answers)
Closed 9 years ago.
I'm trying to round some values as the following examples and need some help to write the math calculation for it:
input -> 25 ÷ 4 = 6.25 output -> 6.5
input -> 15.5 ÷ 4 = 3.875 output -> 4.0
input -> 24.5 ÷ 4 = 6.125 output -> 6.0
any idea how to write the round math procedure please?!
This should do it.
double Divide(double numerator, double denominator)
{
double result = numerator / denominator;
//round to nearest half-integer
result = Math.Round(result * 2, MidpointRounding.AwayFromZero) / 2;
// due to peculiarities of IEEE754 floating point arithmetic
// we need to round again after dividing back by two
// to avoid a result like 1.49999999.
return Math.Round(result, 1);
}
Sorry for not aware the difficulty you encountered, so I guess maybe the different types of floating point number. Following code just does that:
public static decimal RoundedDivide<T>(T a, T b) {
var x=2*Convert.ToDecimal(a)/Convert.ToDecimal(b);
x=((int)(.5m+x)>x?1:0)+(int)x;
return x/2;
}
Two things to note:
I cannot bound a constraint of ValueType, so if you pass objects of reference types, it may throw
If you wish double as the return type, then just change it and also change .5m to .5, Convert.ToDecimal to Convert.ToDouble