Why does Math.Round give me two different values? [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 5 months ago.
Improve this question
class Program {
static void Main(string[] args) {
double d = 120.5;
Console.WriteLine(Math.Round(120.5)); //121
Console.WriteLine(Math.Round(d)); // 120
}
}
When a variable is passed as an argument into Math.Round it produces an answer similar to Convert.ToInt32 where floating numbers are rounded off to the nearest even number if the trailing tenth number is 0.5.
Anyone can kindly explain? Thanks in advance.
Thanks for the answers! I use Replit most of the time, that's the output I got. But seeing the replies, I tested it again in VS and I got both 120.
I guess there's a bug in replit?
Kindly refer to attachments.
enter image description here
enter image description here

I tested your code and returns 120 in two modes. It is good to know that the Math.Round() has features that you can use.
For example, you can say to always round to a number that is further from zero:
double d = 120.5;
Console.WriteLine(Math.Round(d,MidpointRounding.AwayFromZero)); //always 121
or final digit is even:
Console.WriteLine(Math.Round(d,MidpointRounding.ToEven)); //always 120
and other features like MidpointRounding.ToNegativeInfinity, MidpointRounding.ToPositiveInfinity ....

Related

"else cannot start a statement" issue [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 1 year ago.
Improve this question
I tried making a program where every few seconds a random letter dropped. I noticed that the "correct" one dropped to rarely. I made a int correctLetterDrop = Random.Range(0,100) and wanted to artificially increase the chance. The rest of the code handles everything, i just need to assign the correct sprite to check against.
This gives me an error at the .correctSprite; saying "else cannot start a statement". I expect it to just check if the Random.Range returned 7 or less (for a 7% chance) and if it did to set the correct one, and otherwise just set a random one.
if(correctLetterDrop <= 7){
Clone.GetComponent<SpriteRenderer>().sprite = correctLetterScript.correctSprite;
else{
Clone.GetComponent<SpriteRenderer>().sprite = Letters[Letter];
}
}
Check for the first if condition closure }:
if(correctLetterDrop <= 7) {
Clone.GetComponent<SpriteRenderer>().sprite = correctLetterScript.correctSprite;
}else{
Clone.GetComponent<SpriteRenderer>().sprite = Letters[Letter];
}

Bizarre division behavior [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I know at a first glance, this will get a duplicate mention. And it very well might be, but I imagine most people are going to think that the answer is similar to this question: Division returns zero
That is not my case. That case, is dividing a smaller number by a larger number integers and getting 0. That makes sense. What's happening for me does not make sense.
Here's my code:
decimal correctedImageWidth = screenWidth / maxColumnsWithMaxSizeImages;
The value for screenwidth, which is an int, is 1024. The value for maxColumnsWithMaxSizeImages, also an int, is 3.
Somehow, correctedImageWidth becomes 0. Which is odd, because 1024/3 does not equal 0, nor would the rounded off number be 0, like the other SO questions had. You'd think I'd get something like 341. Here's proof that the numbers are what I say they are:
As you can see in my watch. screenWidth is 1024, maxColumnsWithMaxSizeImages is 3. However, dividing these 2 into correctedImageWidth is 0? Is there any reason why I would get 0 from this? I have shown this to colleagues, who are equally as confused. Here's my entire code, perhaps there's something I'm doing? Unlikely, seeing as they're both ints and they both have valid integer values. But obviously there must be something I'm doing? Here's the code:
int maxColumnsWithMaxSizeImages = (int)System.Math.Ceiling(decimal.Divide(1024, 480));
if (maxColumnsWithMaxSizeImages < _minimumImagesPerRow)
{
.....
} else if (maxColumnsWithMaxSizeImages > _maximumImagesPerRow)
{
....
} else
{
//between 2 and 4 columns
var screenWidth = App.ScreenWidth;
decimal correctedImageWidth = (decimal)((decimal)screenWidth / (decimal)maxColumnsWithMaxSizeImages);
decimal test2 = 1024 / 3;
decimal test3 = (decimal)1024 / (decimal)3;
var test = correctedImageWidth;
}
UPDATE
For some reason, it appears that there was a conflict in my variable declarations. Even though I declared them both in different scopes, for whatever reason it was stirring a conflict. After renaming the variable, I get the corrected value.
You've defined the field correctedImageWidth twice, once as int (in the if block) and once as decimal (in the else block).
The screenshot of your Watch window shows the int typed field value, which shows 0 at this point (wasn't assigned since your in the else block). Try watching the decimal typed field, or just hovering your mouse over the other decimal typed field while debugging, it should show you the correct value.

How Math.Round is working? [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 7 years ago.
Improve this question
Recently I found an issue with C# Math.Round that makes me confused:
Math.Round(1.125, 2) == 1.12
However:
Math.Round(1.115, 2) == 1.12
Also
Math.Round(1.225, 2) == 1.23
And
Math.Round(1.215, 2) == 1.22
The last three samples are make sense just want to know how does it work for the first one?
Regarding to the document, if round to nearest even number then why in third sample round to Odd number?
Is anyone has any idea how is rounding the digits?
Updating
As I put the link, I know try to round to nearest even number, I want to know how implementing it (Using shift bytes)?
In the last three examples, it's working as documented. In the first case though, it appears to go wrong:
Math.Round(1.125, 2) == 1.12 // Would expect 1.13
The reason is simple: floats are stored as binary values; not decimal. So it's rounding the binary value that 1.125 has been converted to. In this case it is slightly below 1.125 and thus rounds "the wrong way".

Calculate & subtract percentage in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Well I know this would be a very easy question ,but i am poor at this concept of Maths.
I know what my final answer should be ,but this answer should come after I subtract some specific percentage from another amount. I want to know what that another amount should be.
Making it a bit simpler :
this is my equation : x - 12% of x should result in 250$. I need to find x.
( Please know if I add 12% to 250 , it will be wrong as 12% of 250 would be different from 12% of what that another amount would be ).
Thank you friends.
This is simply algebra (not necessarily even relevant to C#).
Your equation is:
Solve for :
Therefore this can be calculated in C#:
double x = 250/0.88;
Console.WriteLine(x);
which outputs 284.090909090909

DateTime.toString shows with time when output to Email [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 8 years ago.
Improve this question
Edit: I figured it out, ill explain here.
I had the following code filled in
I didn't want the time part, just the year, day and month
//model
public DateTime Indate { get; set; } //this is filled in by a form with "01/01/2015"
//controller
(DateTime)person.Indate).ToString("dd-MM-yyyy")
What i Failed to notice is that this only applied for that line code, i missed out on adding that line EVERYWHERE.
htmlBody1 += "<tr><td style=\"width: 225px\">Starting Date Project </td><td>: " + person.Indate + "</td></tr>";
This was my incorrect line, i forgot to add the .ToString("dd-MM-yyyy") there aswell
I solved it.
thanks to Damien_The_Unbeliever, for pointing out that it does not remember your format if you output it sometimes. this made me realise i had to specficly point out out everywhere.
I just ran this code and it outputs exactly what you you want:
You can also use ToString("d"), which will output 28.10.2014 (based on your culture), or ToShortDateString(), which will output the same.
You're missing a parenthesis in the code shown. Your formatting statement isn't valid C# code.
With that saud, this works for me:
DateTime date = DateTime.Now;
System.Console.WriteLine(((DateTime)date).ToString("dd-MM-yyyy"));

Categories