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
22 odpowiedzi
+ 5
You can use Math.Round(number, numberOfDigits)
+ 5
Lisa they used
MidpointRounding.AwayFromZero
+ 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}");
...
+ 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
+ 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
+ 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.
+ 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.
+ 3
Stephen Cloe
Try using the
price = Math.Round(price, 2, MidpointRounding.toEven);
+ 3
BroFar
Did it work? I think using double causes a floating point issue.
+ 3
Try it in my code Lisa switch 3 to 2 and MidpointRounding.toEven rounds the decimal point up or down.
+ 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.
+ 3
Lisa
I agree 👍 not exactly fair when there are other not clearly explained functions such as
MidpointRounding.AwayFromZero
MidpointRounding.ToEven
+ 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?
+ 2
BroFar
In the Kaleidoscopes task, I mean, not in your code.
I tried to solve the task using double, but case 2 fails.
+ 2
Stephen Cloe I hope this helped you with your task above.
+ 2
BroFar AwayFromZero worked, thank you.
+ 2
Thank you to both of you for your help!
+ 2
Lothar Oh, thank you, I will keep this in mind. And as a code bit 😁
+ 2
Rahul D Coderz You can read the previous replies.
+ 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);
}