Why is Math.Round() not resulting as I expect in C# [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why does Math.Round(1.444445M, 2, MidpointRounding.AwayFromZero) return 1.44 instead of 1.45?

Because this is how rounding works.
Like #Sweeper said, 1.444445 is closer to 1.44 than to 1.45.
You take a number 1.444445.
Now you want to round it to 2 decimal places, so select 2 digits after dot: 1.[44]4445
Then look at the next digit after [44], which is 4 also
4 < 5, so rounding should not be applied. Edit: I mean it will stay the same [44], rest of number will be zeroed ofcourse

Your expectation seems to be similar to this recursive function that will result in 1.45 after a sequence of Math.Round.
double RecursiveMathRound(double val, int precisionLevel, int decimalPlace)
{
return (precisionLevel <= decimalPlace)
? Math.Round(val, precisionLevel, MidpointRounding.AwayFromZero)
: RecursiveMathRound(Math.Round(val, precisionLevel, MidpointRounding.AwayFromZero), precisionLevel - 1, decimalPlace);
}
RecursiveMathRound(1.444445d, 5, 2);
However, any form of rounding is less 'accurate' or 'correct' then a value that was not rounded at all. The RecursiveMathRound snippet above produces a less accurate or correct approximation because of the series of rounding occurrences.

Related

Cant use decimals when defining a value. C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I have these two lines:
Int32 val3 = 8;
Int32 val4 = 0.66;
The first line works, but the second does not. I don't know why, and I don't know how I would go to fix this or what to search for.
Try using
double val4 = 0.66
Int is for numbers without decimal points...
0.66 is not int32, it is float because it has dot (0.66), only natural numbers are Int
simply use
float val4 = 0.66f
or
var val4 = 0.66
C# recognize it correctly as float at compile time

Math.Round decimal in Visual Studio C# not rounding [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I would like my calc to return a decimal that is two places.
I Googled and found Math.Round function.
I applied it to my code in several places i.e. after the variables are declared, before the return statement, before the calculation line, and after the calculation line, but get $251.333333333333 as my result.
My code looks like this:
public decimal finalCalc()
{
decimal calcNumber3;
decimal g = firstPartofCalc();
decimal h = secondPartofCalc();
if (rbMonthlyPay.Checked)
{
Math.Round(calcNumber3 = (((g + h) * 52) / 12), 2) ;
}
else
{
calcNumber3 = g + h;
}
return calcNumber3;
}
Calcs 1 and 2 return decimal results. I know that there are a few other ways of getting the same result including returning the result as a string. But, as a newbie to the coding thing, I would really love to understand why this is not working.
TIA
Vickie
You are not assigning calculated value to calcNumber3 properly, Change it to:
calcNumber3 = Math.Round((((g + h) * 52) / 12), 2) ;

Why is the number of milliseconds between now and 1970 showing as around 800? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Here is the code I am using. It seems simple but the number it returns is incorrect:
var a = DateTime.Now;
var b = new DateTime(1970, 1, 1);
var c = a.Subtract(b);
var d = c.Milliseconds;
return d;
Does anyone have any suggestions as to what I am doing wrong?
The Milliseconds property gives you the milliseconds component of the time span (in other words, the millisecond-of-second), not the total number of milliseconds in the timespan. Its magnitude will always be less than 1000, since it's how many milliseconds are left when you have taken all the whole seconds away from the time span.
For what you want, use TotalMilliseconds.
You have to use c.TotalMilliseconds instead of c.Milliseconds;

What relevance is 1.307 to the series 1 + 1/2 + 1/3 + 1/4... + 1/n [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm currently working my way through the Nakov book, Fundamentals of Computer Programming in C#. In Chapter 4 question 12 states:
Write a program that calculates the sum (with the precision of 0.001) of the following sequence: 1 + 1/2 - 1/3 + 1/4 - 1/5 + …
It seemed to me to be a relatively straightforward question. The series is a diminishing fraction that does not have an asymptote. Stopping the loop at a certain point due to diminished changes in value meets the precision requirements AFAIC. However, the solution given in both the Hungarian and English versions of the book makes reference to an obscure (to me) value of 1.307. As follows:
Accumulate the sum of the sequence in a variable inside a while-loop (see the chapter "Loops"). At each step compare the old sum with the new sum. If the difference between the two sums Math.Abs(current_sum – old_sum) is less than the required precision (0.001), the calculation should finish because the difference is constantly decreasing and the precision is constantly increasing at each step of the loop. The expected result is 1.307.
Can someone explain what this might mean?
Note that header contains "harmonic sequence" that has no limit.
But question body shows alternate sign sequence that converges towards value 2 - ln(2)
The expected result is 1.307.
I think they are simply saying what the result of the calculation is, so you can check your answer.
The sequence you've got
1 + 1/2 - 1/3 + 1/4 + ...
is the same as the Alternating Harmonic Series on Wikipedia, except with the signs from 1/2 onwards flipped:
1 - 1/2 + 1/3 - 1/4 + ... = ln 2
and the natural logarithm of 2, ln 2, = 0.693. Hence your 1.307 here = 2 - ln 2.

How to do Division in a Fractional Calculator? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm completely unsure on how to do it. I've searched but can't find a simple answer.
I've done the multiplication and I know its similar to it. Need some help. I want to know how to do division for two fractions.
My Multiply module:
{
answerDenominator = num1Denominator * num2Denominator; //Multiply both denominators
answerNumerator = ((num1Whole * num1Denominator) + num1Numerator) * //multiply the whole number by the denominator and add the numerator to it
((num1Whole * num2Denominator) + num2Numerator); //multiply the whole number by the second denominator, then add the second numerator, multiply these two answers together
answerWhole = answerNumerator / answerDenominator;
answerNumerator = answerNumerator % answerDenominator;
}
Let that we have to make the following division:
(a/b):(c/d)
This is equal to
(a/b)*(d/c)
That being said the division can simply be done like below:
static double CalculateDivisionResult(double a, double b, double c, double d)
{
return (a/b)*(d/c);
}
In the above:
a is the num1Numerator.
b is the num1Denominator.
c is the num2Numerator.
d is the num2Denominator.
The most important thing that you should pay attention on the above is the fact that we use double. Why we do so?
Let that a=3, b=7, c=4 and d=5:
Then
(a/b)*(d/c) = 15/28
If you had chosen to represent your number as integers, int a=3, then the above would be obvious 0. Representing them as doubles we can overcome this.

Categories

Resources