C
C#4mo ago
Ragman

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
No description
29 Replies
leowest
leowest4mo ago
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?
Ragman
Ragman4mo ago
I see how would you suggest going about this? wait nevermind i see
leowest
leowest4mo ago
you understand, what happens to 12.50 in this scenario? it becomes what
Yehor
Yehor4mo ago
Do you even try to use chat gpt (or another AI)?..
Ragman
Ragman4mo ago
yes i see
leowest
leowest4mo ago
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
Ragman
Ragman4mo ago
thats what my proffesor said
leowest
leowest4mo ago
so you should not convert it twice to different types
Ragman
Ragman4mo ago
yes i see, do i need to convert the double of the hours into a decimal or something to be able to multiply them?
Yehor
Yehor4mo ago
If he corrects the mistake, then is it worth learning from? Hes also explaining problems.
leowest
leowest4mo ago
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
Ragman
Ragman4mo ago
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
leowest
leowest4mo ago
ok, so the answer is yes in this scenario u would have to cast one of the other you know what casting it?
Ragman
Ragman4mo ago
i have no idea, this is the first assignment and he kind of just put is right in it without explaining a ton
leowest
leowest4mo ago
casting is when you convert type A to type B, given this might not be possible in some cirustances
Ragman
Ragman4mo ago
ok i see so ill need to convert the double into a decimal to be able to multiply them together?
leowest
leowest4mo ago
for example
MODiX
MODiX4mo ago
leowest
REPL Result: Success
double hours = 10.0;
decimal work = 12.50m;
Console.WriteLine((decimal)hours * work);
double hours = 10.0;
decimal work = 12.50m;
Console.WriteLine((decimal)hours * work);
Console Output
125.00
125.00
Compile: 406.143ms | Execution: 25.898ms | React with ❌ to remove this embed.
leowest
leowest4mo ago
if you notice inside the console.writeline I am casting hours to decimal
Ragman
Ragman4mo ago
No description
Ragman
Ragman4mo ago
somewhat like this?
leowest
leowest4mo ago
that is converting not casting
Ragman
Ragman4mo ago
oohhh ok
leowest
leowest4mo ago
You can read more detailed about it here https://www.geeksforgeeks.org/c-sharp-type-casting/ edited this one is easier to read
Ragman
Ragman4mo ago
ok thank you i think i get it now
leowest
leowest4mo ago
so do u see how I did in the example above?
Ragman
Ragman4mo ago
yes i was watching an indian guy on youtube but i wasnt quite sure what he meant but you explained it better
leowest
leowest4mo ago
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
Ragman
Ragman4mo ago
yeah that probably wouldve been easier but its already done