-
November 6th, 2008, 04:26 AM
#1
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
-
November 6th, 2008, 04:42 AM
#2
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.
-
November 6th, 2008, 05:02 AM
#3
Re: Problem using Math.Round()
Thanks all sorted it works- I had edited the wrong method. Thanks for your help.
-
November 6th, 2008, 08:37 AM
#4
Re: Problem using Math.Round()
Originally Posted by MNovy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|