Page 1 of 1

OT: C# Homework Help

Posted: Fri Jan 28, 2011 4:58 am
by MrCheckOne2
Alright so I'm takin an intro course to C#
We did our Hello World program first and that was very simple and went by with no issues (I'm COMPLETELY new to programming...not a single clue or prior exposure to it)

Alright well I found out what my original problem was but now I have a question.

So I was declaring a constant as 9/5 thinking it would take whatever the value of that fraction was. I converted it to decimal instead and it's working fine, but now my question is, can I declare a constant in a way that I can enter a fraction and it'll use the value of it??

Re: OT: C# Homework Help

Posted: Fri Jan 28, 2011 5:53 am
by MaryvalesFinest
I took a programming class my senior year in highschool, are they using that pink colored book still?

Re: OT: C# Homework Help

Posted: Fri Jan 28, 2011 6:01 am
by MrCheckOne2
I'm not attending your high school, wouldn't be able to tell you.

Re: OT: C# Homework Help

Posted: Fri Jan 28, 2011 6:02 am
by MaryvalesFinest
No I took a computer programming when I was in highschool, was wondering if you are using the same book cause I might be able to help than.

Re: OT: C# Homework Help

Posted: Fri Jan 28, 2011 6:04 am
by MrCheckOne2
Nope, not a pink book.

Re: OT: C# Homework Help

Posted: Fri Jan 28, 2011 6:07 am
by MaryvalesFinest
Ah alright, I'm not saying this is right cause that was like 4 years ago but if I remember I think constants were talked about at the start of course so that might work, you can always change the code back if it's wrong I think, good luck.

Re: OT: C# Homework Help

Posted: Fri Jan 28, 2011 7:49 am
by morol
If you divide two integers using / operator it returns an integer, and 5 and 9 are both implicitly converted to int (in fact, they ARE ints, so there is no conversion afterall). If you want them to be treated as floats, You need to cast them:

float some_constant = (float) 9 / (float) 5

'/' operator, when given atl least one float (as above) returns a float. Give it a try,