round decimal values up to the nearest of 0.01? [duplicate] - c#

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 round decimal value up to nearest 0.05 value??, the linked SO post also discusses the similar topic, but its not the output i expected.
I need to convert the decimal values like this
16.489-->16.49
16.482-->16.48
16.425-->16.43
7.67 --> 7.67 (no conversion)
I can use the below C# method to convert the values
Math.Round(16.482*20)/20;
But this method not works for me, it gives the following results
16.489-->16.5
16.482-->16.5
7.67 --> 7.7
16.425-->16.45
whats the elegant way in c# to do this.

Math..::.Round Method (Decimal, Int32, MidpointRounding)
Rounds a double-precision floating-point value to the specified number of fractional digits. A parameter specifies how to round the value if it is midway between two other numbers.
Math.Round(1.489,2,MidpointRounding.AwayFromZero)

Did you try
Math.Round(16.482*200)/200;

Related

Why does my double round my variables sometimes? [duplicate]

This question already has answers here:
Difference between decimal, float and double in .NET?
(18 answers)
Closed 3 years ago.
I really dont unterstand why, but my double array just sometimes round my variables, even though it shouldn't. The weird thing is, that it only does this sometimes, as you see in the picture. All of the array elements should have this long precision. The Variable "WertunterschiedPerSec" is also at this precision every time, but still, if i add it to Zwischenwerte[i], then it sometimes just get less precisie, even though i dont do anything anywhere. Does anybody know why?
I would suggest using a decimal, but let's get into the exact details:
double, float and decimal are all floating point.
The difference is double and float are base 2 and decimal is base 10.
Base 2 numbers cannot accurately represent all base 10 numbers.
This is why you're seeing what appears to be "rounding".
I would use the decimal variable instead of double because you can basically do the same functions as a double except for using the Math function. Try to use Decimals and if you need to convert than use:
Double variable = Convert.ToDouble(decimal var);
Decimals are meant for decimals so they will hold more information than a float or decimal

Is the double data type not suitable for my data / calculation? [duplicate]

This question already has answers here:
Is double Multiplication Broken in .NET? [duplicate]
(6 answers)
Closed 6 years ago.
my unit test failed because of this, so I wonder if I am using the right datatype?
Reading the specs of the double I'd think it should be ok, but this is what's happening:
I'm reading a string from a file with value 0,0175 (comma as decimal sep.)
then I convert it to a double and then multiply it by 10000.
The function which does the multiply is this:
private static double? MultiplyBy10000(double? input)
{
if (!input.HasValue)
{
return null;
}
return input.Value*10000;
}
next is from the immediate window:
input.Value
0.0175
input.Value*10000
175.00000000000003
And that is where my unit test fails, because I expect 175.
Is the double not accurate enough for this?
Checked other values too:
input.Value*1
0.0175
input.Value*10
0.17500000000000002
input.Value*100
1.7500000000000002
input.Value*1000
17.5
input.Value*10000
175.00000000000003
The weird thing is, I have 12 testcases
0,0155
0,0225
0,016
0,0175
0,0095
0,016
0,016
0,0225
0,0235
0,0265
and assert 4 of these, and the other 3 don't have this behaviour
Is the double not accurate enough for this?
No, doubles are floating point numbers, which are inaccurate by design. You can use decimal which is accurate and more suitable if you need exact numbers.
Floating point numbers where the value to the right of the decimal point are not powers of 2 cannot be accurately represented. Although you've got what looks like 0.0175 there's actually more data there.
After you've scaled the number up use Math.Round to trim off date on the right off the decimal point.

Why exactly 0.09f - 0.01f == 0.0800000057f? [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
I know that floating point variable stores the number in a sign-exponent-fraction format (as it's stated in the IEEE 754), it's never precise and I should probably never compare two floats without specifying the precision.
But why exactly 0.09f - 0.01f gives you the value of 0.0800000057f? What exactly happens in under the hood of the .NET VM and in the memory when I do that subtraction?
0.09 is represented as 00111101101110000101000111101100 in ieee-754, which is closest to the decimal value of 0.09000000357627869
0.01 is represented as 00111100001000111101011100001010 in ieee-754, which is closest to the decimal value of 0.009999999776482582
Their subtraction yields 00111101101000111101011100001011 which is closest to the decimal value of 0.08000000566244125
You can see how the actual subtraction is done here

Why Math.Round() works differently in C# [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
First I noticed that Math.Round() doesn't round for 2 digits as I as for:
double dd = Math.Round(1.66666, 2);
Result:
dd = 1.6699999570846558;
and then I created a new project with same .NET framework and the result is 1.67 now as it should been in the first place.
I've never seen Round behaving like this before, what is causing this problem?
Like the other comments mentioned, use decimal to hold the returned value:
decimal dd = Math.Round(1.66666M, 2);
The issue you described has nothing to do with the Round() function. You can read a bit about how the floating point numbers & fixed point numbers work, but the short explanation is:
With floating point variables (e.g. double), you cannot guarantee the precision of the number you save it them. So when you save something like 1.67 in a variable of the type double and then you check the value later on, therer is no guarantee you will get exactly 1.67. You may get a value like 1.66999999 (similar to what you got) or like 1.6700000001.
Fixed point variables (e.g. decimal) on the other hand, will give you that precision, so if you save 1.67, you will always get 1.67 back.
Please note that the Round() function returns the same type that you pass to it, so to make return a decimal, you need to pass it 1.66666M which is a decimal value rather than 1.66666 which is a floating point number.

Problem In OutPut Of Divided Number [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Divide problem
Why does c# show the output of this code as 0??
MessageBox.Show((5/6).ToString);
Because unless you specify that you want the operation to result in a Double the operation results in an Integer, and so the fractional result is dropped and you are left with just the whole number of 0.
5/6 is basically integral division, which turns out to be 0. The type of both operands is int.
I think what you want is : 5.0/6.0.
In fact, 5.0/6.0, 5/6.0, 5.0/6, all would give same result. That is, as long as, one operand is double, it would be a double division, and the type of the result would be double as well.
It is dividing an integer by and integer and will return an integer, I believe it always returns the floor value. Try Messagebox.Show((5.0/6.0).ToString());
Because you are doing integer division. If you want non-integer division you should do something like 5 / 6d
The compiler assumes that the numbers are Int, which must be whole numbers. Thus, it is rounding the answer. To return the decimal answer, use this:
MessageBox.Show((5d/6d).ToString());

Categories

Resources