C
C#10mo 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
leowest10mo 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
RagmanOP10mo ago
I see how would you suggest going about this? wait nevermind i see
leowest
leowest10mo ago
you understand, what happens to 12.50 in this scenario? it becomes what
Yehor
Yehor10mo ago
Do you even try to use chat gpt (or another AI)?..
Ragman
RagmanOP10mo ago
yes i see
leowest
leowest10mo 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
RagmanOP10mo ago
thats what my proffesor said
leowest
leowest10mo ago
so you should not convert it twice to different types
Ragman
RagmanOP10mo 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
Yehor10mo ago
If he corrects the mistake, then is it worth learning from? Hes also explaining problems.
leowest
leowest10mo 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
RagmanOP10mo 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
leowest10mo 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
RagmanOP10mo 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
leowest10mo ago
casting is when you convert type A to type B, given this might not be possible in some cirustances
Ragman
RagmanOP10mo ago
ok i see so ill need to convert the double into a decimal to be able to multiply them together?
leowest
leowest10mo ago
for example
MODiX
MODiX10mo 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
leowest10mo ago
if you notice inside the console.writeline I am casting hours to decimal
Ragman
RagmanOP10mo ago
No description
Ragman
RagmanOP10mo ago
somewhat like this?
leowest
leowest10mo ago
that is converting not casting
Ragman
RagmanOP10mo ago
oohhh ok
leowest
leowest10mo 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
RagmanOP10mo ago
ok thank you i think i get it now
leowest
leowest10mo ago
so do u see how I did in the example above?
Ragman
RagmanOP10mo ago
yes i was watching an indian guy on youtube but i wasnt quite sure what he meant but you explained it better
leowest
leowest10mo 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
RagmanOP10mo ago
yeah that probably wouldve been easier but its already done
Want results from more Discord servers?
Add your server