CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    17

    Problem using Math.Round()

    Hi,

    I am having a problem using Math.Round(). My program reads in a decimal from a windows form text box,converts this to a double. Then I am trying to round off the double to the nearest whole number. However this is not working it does not round off the number at all. Here is my code:


    double hedgeRatio = Convert.ToDouble(HR.Text);
    hedgeRatio = System.Math.Round(hedgeRatio,0);

    I am using visual c# 2008 express edition to build my program. Also is it possible to read in a number from the user in a windows form instead of using a Text Box and then converting to a double?

    Thanks All, any help or suggestions would be great.

    Cheers,
    The Big Ham

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Problem using Math.Round()

    It should work. Ensure to have a comma instead of a dot in your string!

    Code:
    double hedgeRatio = Convert.ToDouble("3,54");
    hedgeRatio = System.Math.Round(hedgeRatio, 0);
    
    Console.Write(hedgeRatio); // prints 4
    Console.Read();
    Last edited by MNovy; November 6th, 2008 at 04:49 AM.

  3. #3
    Join Date
    Nov 2008
    Posts
    17

    Re: Problem using Math.Round()

    Thanks all sorted it works- I had edited the wrong method. Thanks for your help.

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Problem using Math.Round()

    Quote Originally Posted by MNovy View Post
    It should work. Ensure to have a comma instead of a dot in your string!

    Code:
    double hedgeRatio = Convert.ToDouble("3,54");
    hedgeRatio = System.Math.Round(hedgeRatio, 0);
    
    Console.Write(hedgeRatio); // prints 4
    Console.Read();
    Even though Germany uses a comma instead of a decimal, it doesn't mean that it is always the correct answer. In the US, we use a decimal. So your example would fail if the current culture is the US.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured