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) ;
Related
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
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've read a few posts and I understand how double arithmetic doesn't always return the results you would expect.
Why is floating point arithmetic in C# imprecise?
My problem is different though because I'm checking to see if two numbers are the same and I'm getting false when I would expect true.
I've also read this What Every Computer Scientist Should Know About Floating-Point Arithmetic, but am still having trouble understanding why two seemingly equal double variables are showing unequal.
UPDATE: They answer below helped me understand that the value displayed by the debugger wasn't the 'whole story'. That is why the two floats seemed equal. The debugger was showing equal values when hovering the variables.
Default string format is not precise for float numbers, should use "G17"
double a = 17.125 / 3.0;
double b = 17.12499999999999 / 3.0;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(a.ToString("G17"));
Console.WriteLine(b.ToString("G17"));
Console.WriteLine(a == b);
Console.WriteLine(Convert.ToString(*(long*)&a, 2));
Console.WriteLine(Convert.ToString(*(*)&b, 2));
Result:
5.70833333333333
5.70833333333333
5.708333333333333
5.7083333333333295
False
100000000010110110101010101010101010101010101010101010101010101
100000000010110110101010101010101010101010101010101010101010001
Usually to compare 2 float numbers, you can use a small error number
Console.WriteLine(Math.Abs(a - b) < 0.0000001);
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have this following code to calculate the percentace in perst, but when i run this code I always get o as value in perst for all values of qxount and acount.
int perst;
int qcount;
int acount;
perst = (acount / qcount) * 100;
When dividing integers the result will be an integer. This means that you are expecting a value such as 0.75 (which you appear to be thinking you'll multiply by 100 to get a percentage) then the integer value returned will be only the 0 which is the leading integer. The remainder would be available with the % modulus operator.
However, get a percentage like you seem to want, you'll need to divide using doubles or float values.
double perst;
double qcount;
double acount;
perst = (acount / qcount) * 100;
The MSDN article on the division operator - Good idea to read.
I think you have some integer rounding issues. Try something like this.
int perst;
int qcount = 100;
int acount = 5;
perst = Convert.ToInt32(((double)acount / (double)qcount) * 100);
While rounding is almost certainly the mistake (after assigning values to the variables) the following works just fine:
int a = 62;
int b = 235;
int percentage = 100*a/b;
Console.WriteLine(percentage);
You don't need to use doubles. This rounds the percentage towards zero. If you need more precise results, go with double or single.