Rounding Doubles in C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Rounding Doubles in C#

I'm trying to do the Kaleidescope Code Coach, but I'm getting errors returned for some of the cases because the decimal values are in the thousandths place and not rounded to hundredths, as the program wants. How do I round the value to hundredths? (Additionally, how does one link Code Coach code? They don't show up under "My Code Bits") Thank you

19th Sep 2023, 2:05 PM
Stephen Cloe
Stephen Cloe - avatar
21 Answers
+ 5
You can use Math.Round(number, numberOfDigits)
19th Sep 2023, 2:18 PM
Lisa
Lisa - avatar
+ 5
Lisa they used MidpointRounding.AwayFromZero
19th Sep 2023, 2:58 PM
BroFar
BroFar - avatar
+ 5
Stephen Cloe , you can solve it by using this: > remove both lines in code that contain the rounding. > for the output we use string interpolation, which can be given also the decimal precision (this is doing the rounding) like: ... Console.WriteLine(
quot;{price:0.00}"); ...
19th Sep 2023, 3:26 PM
Lothar
Lothar - avatar
+ 4
Stephen Cloe the easiest way is using the built in Math.Round() function .. or Math.Round(va, 3, MidpointRounding.toEven) for decimal rounding https://code.sololearn.com/cpRmIHny5waY/?ref=app
19th Sep 2023, 2:18 PM
BroFar
BroFar - avatar
+ 4
Lisa in most cases double is preferred over float given decimal points as far as c# for precision... I also have a tendency to use float
19th Sep 2023, 3:12 PM
BroFar
BroFar - avatar
+ 4
Rahul D Coderz You can read the previous replies and find out what others have suggested and what turned out to be the actual problem.
21st Sep 2023, 7:21 AM
Lisa
Lisa - avatar
+ 4
Rahul D Coderz, do you have an actual point, or are you just here to flash your alleged credentials and argue with people? The problem seems to be solved, so I suggest moving on.
21st Sep 2023, 11:29 AM
HonFu
HonFu - avatar
+ 3
Stephen Cloe Try using the price = Math.Round(price, 2, MidpointRounding.toEven);
19th Sep 2023, 2:43 PM
BroFar
BroFar - avatar
+ 3
BroFar Did it work? I think using double causes a floating point issue.
19th Sep 2023, 2:47 PM
Lisa
Lisa - avatar
+ 3
Try it in my code Lisa switch 3 to 2 and MidpointRounding.toEven rounds the decimal point up or down.
19th Sep 2023, 2:49 PM
BroFar
BroFar - avatar
+ 3
BroFar Rounding away from zero worked. But it's not a fair task, if one has to guess the expected rounding procedure. I just used float, and the default rounding procedure worked.
19th Sep 2023, 2:58 PM
Lisa
Lisa - avatar
+ 3
Lisa I agree 👍 not exactly fair when there are other not clearly explained functions such as MidpointRounding.AwayFromZero MidpointRounding.ToEven
19th Sep 2023, 3:03 PM
BroFar
BroFar - avatar
+ 3
BroFar Is it possible that they intended us to use a different data type or was it just chance that floats worked for me?
19th Sep 2023, 3:06 PM
Lisa
Lisa - avatar
+ 2
BroFar In the Kaleidoscopes task, I mean, not in your code. I tried to solve the task using double, but case 2 fails.
19th Sep 2023, 2:52 PM
Lisa
Lisa - avatar
+ 2
Stephen Cloe I hope this helped you with your task above.
19th Sep 2023, 3:14 PM
BroFar
BroFar - avatar
+ 2
BroFar AwayFromZero worked, thank you.
19th Sep 2023, 3:28 PM
Stephen Cloe
Stephen Cloe - avatar
+ 2
Thank you to both of you for your help!
19th Sep 2023, 3:28 PM
Stephen Cloe
Stephen Cloe - avatar
+ 2
Lothar Oh, thank you, I will keep this in mind. And as a code bit 😁
19th Sep 2023, 3:35 PM
Stephen Cloe
Stephen Cloe - avatar
+ 2
Rahul D Coderz You can read the previous replies.
21st Sep 2023, 6:01 AM
Lisa
Lisa - avatar
+ 1
Lisa It cuts off the thousandth digit, but it doesn't round it up (i.e. my raw output is 14.445, running it through the Math.Round method changes it to 14.44, which is incorrect) Here is my code: static void Main(string[] args) { double quantity=Convert.ToDouble(Console.ReadLine()); double price=0; if (quantity>1) { price=(quantity*5*0.9*1.07); price=Math.Round(price, 2); } else { price=quantity*5*1.07; price=Math.Round(price, 2); } Console.WriteLine(price); }
19th Sep 2023, 2:39 PM
Stephen Cloe
Stephen Cloe - avatar