none
Problem to show the number's decimal? RRS feed

  • Question

  • I have problem with the code below this text. It doesn't show any decimals, only the number 0. Can someone help me to correct this code, so it shows also the decimals. In this case it enough with 2 decimals.

    double answerCount;

    answerCount = 5 / 20;
    answerCount = Math.Round((Double)answerCount, 2);

    Friday, October 18, 2019 9:09 PM

Answers

All replies

  • Format the value as the value is 0.00.

    double answerCount;
    
    answerCount = 5 / 20;
    answerCount = Math.Round((Double)answerCount, 2);
    Console.WriteLine(answerCount.ToString("F"));

    And

    double answerCount = 5 % 20;
    answerCount = Math.Round(answerCount, 2);
    Console.WriteLine(answerCount.ToString("F"));


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 18, 2019 11:53 PM
    Moderator
  • The ‘/’ operator works differently for integer and floating-point numbers.

    Try this:

       answerCount = 5.0 / 20;

    • Marked as answer by EKH10 Saturday, October 19, 2019 6:30 PM
    • Unmarked as answer by EKH10 Saturday, October 19, 2019 6:30 PM
    Saturday, October 19, 2019 7:18 AM
  • Thanks for your answers. I got it the work.
    • Marked as answer by EKH10 Saturday, October 19, 2019 6:31 PM
    Saturday, October 19, 2019 6:31 PM