Cyber-Drugs
asked on
C# - Convert String containing number to only display 2 decimal places
Hi guys 'n gals,
How would I convert a string which contains a number, to only show 2 decimal places, but retain whether it was negative/positive?
I tried this:
string s = "-115.000208"; // I want to display "-115.00"
Convert.ToDouble(s);
string.Format("{###0.00}", s);
MessageBox.Show(s.ToString ());
But unfortunately, that throws an error of:
Input string was not in a correct format.
Anybody able to help me out please?
Cheers!
How would I convert a string which contains a number, to only show 2 decimal places, but retain whether it was negative/positive?
I tried this:
string s = "-115.000208"; // I want to display "-115.00"
Convert.ToDouble(s);
string.Format("{###0.00}",
MessageBox.Show(s.ToString
But unfortunately, that throws an error of:
Input string was not in a correct format.
Anybody able to help me out please?
Cheers!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Agarici,
TryParse gave me a similar error...
zer0_ring,
Tried the first one and it does the trick, I may actually make use of a mix of both, that way I get the rounded off number as well as 2 decimal places.
Cheers!
TryParse gave me a similar error...
zer0_ring,
Tried the first one and it does the trick, I may actually make use of a mix of both, that way I get the rounded off number as well as 2 decimal places.
Cheers!
hth,
A.