lets say i have an string str="45.6767676"; now in output i need to show as 45.67 if the string as str= "4"; then show the output as 4 is there any 开发者_运维技巧built in function to do this. thanks
double value = double.Parse(str);
Console.WriteLine(value.ToString("##.##"));
See here: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Just check if the string contains a decimal. If it does, then make use of the options provided in the link posted by Jouke. If it does not, then don't do anything.
Console.WriteLine(Double.Parse("4.676767").ToString("0.##"));
精彩评论