Cant use decimals when defining a value. 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 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

Related

Why is the int.TryParse returning false everytime? [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
The testCards string picks up the value from web.config. But every time I try to use int.tryParse it gives a false value when I try to parse the string testCards. Any idea what I might be missing?
<add key ="TestCards" value ="4987654321098769,4111111111111111,4987654321098769"/>
string testCards = ConfigurationManager.AppSettings["TestCards"];
int flag=0
bool isSuceeded=false
isSuceeded = int.TryParse(testCards, out flag);
It's not an int! It has commas in it, and if it didn't have commas the number is much bigger than an int can hold in any .NET platform.
I'm gathering from the name TestCards that these are intended to be credit card numbers? In that case they should be strings.

how to fix math random method group to float [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
heres the error Argument 2: cannot convert from 'method group' to 'float'
heres the code
public Transform[] Spawns;
Random.Range(1, Spawns.GetLength);
Array.GetLength is the name of a method, meaning you need to supply a list of arguments enclosed in () to invoke it:
Random.Range(1, Spawns.GetLength(0));
That being said, the more idiomatic solution for assessing the length of a 1-deminsional array in C# would be to just use the Length property:
Random.Range(1, Spawns.Length);

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 does modulo 100 stop returning a two digit date this year? [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 5 years ago.
Improve this question
So this worked for the last few years:
int currentYear = new DateTime().Year % 100;
But this year instead of 18 it returns 1.
I've fixed it with a different bit of code but why did it suddenly stop working? What's special about 2018?
Edit: Typo in the code provided to me, should have checked it myself. Lesson learned. Thanks to those who answered and feel free to delete / close the question.
Because
int year = new DateTime().Year;
returns 1. This behaviour is described in the Documentation:
DateTime dat1 = new DateTime();
// The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));
// The following method call displays True.
Console.WriteLine(dat1.Equals(DateTime.MinValue));
To get the year you need to use the current time. Use
int currentYear = DateTime.Now.Year % 100;
to solve your problem.

How to get the first value of a variable in C# without using array [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 6 years ago.
Improve this question
Main()
{
int X=10;
X=20;
X=30;
Console.WriteLine ("X :"+X);//will print 30.
}
How to get the first value of X without using array.
I want the result as
X=10
Imagine you have a plate X and you put potatoes on it 10
int X=10;
then you remove the potatoes and put salad 20 on it
X=20;
then you remove the salad and put a steak 30 on it
X=30;
and now you ask yourself how do I serve potatoes to your guest. You guest will receive the steak because it is the last value that you put on the plate.
Console.WriteLine ("X :"+X);//will print 30.
I would suggest to use a List. It would be the equivalent of a plate with a memory.
List<int> x_es = new List<int>();
x_es.Add(10);
x_es.Add(20);
x_es.Add(30);
Now you can serve whatever you have placed already before on your plate
x_es[0];
If you really want to avoid array or List you need further plates/variables to store the content temporarily:
int X=10;
int mem_1 = X;
X=20;
int mem_2 = X;
X=30;
Console.WriteLine ("X :"+mem_1);//will print 10.

Categories

Resources