math
im brand new to C# and Im making a wage calculator but the math is wrong, it keeps rounding the output number so its wrong
29 Replies
so imagine you have a user input that is 12.50, decimal places, you convert it to decimals, great, then right before u calculate it, you convert it to integer, which have no decimal place, what happens?
I see
how would you suggest going about this?
wait nevermind i see
you understand, what happens to 12.50 in this scenario?
it becomes what
Do you even try to use chat gpt (or another AI)?..
yes i see
dont do that for learning is not good. chatgpt is not source of truth and should not be used if you're learning with barely any knowledge of what you're doing
so it turns into 12, it loses the decimal places
thats what my proffesor said
so you should not convert it twice to different types
yes i see, do i need to convert the double of the hours into a decimal or something to be able to multiply them?
If he corrects the mistake, then is it worth learning from? Hes also explaining problems.
no its not because he would not understand why it was correct, but that it now works, if u want to discuss that, feel free to ask in #chat why not use chatgpt as some one just starting with c#
well it would be ideal to keep it to the same type, normally u would use decimal for precision
so I would use decimal for the hours as well
i guess that makes sense, in the description of the assignment it says to use double for the hours
it says look it up how to do the math portion on the assignment which isnt very helpful
ok, so the answer is yes in this scenario u would have to cast one of the other
you know what casting it?
i have no idea, this is the first assignment and he kind of just put is right in it without explaining a ton
casting is when you convert type A to type B, given this might not be possible in some cirustances
ok i see so ill need to convert the double into a decimal to be able to multiply them together?
for example
leowest
REPL Result: Success
Console Output
Compile: 406.143ms | Execution: 25.898ms | React with ❌ to remove this embed.
if you notice inside the console.writeline I am casting hours to decimal
somewhat like this?
that is converting not casting
oohhh
ok
You can read more detailed about it here https://www.geeksforgeeks.org/c-sharp-type-casting/ edited this one is easier to read
ok thank you
i think i get it now
so do u see how I did in the example above?
yes
i was watching an indian guy on youtube but i wasnt quite sure what he meant but you explained it better
no worries
glad you understood it
by the way ragman u could have used Convert.ToDecimal in there as well, it would work its just not ideal way of doing it in that scenario but might be just what your teacher wants
if he have not explained to you about casting
yeah that probably wouldve been easier but its already done