happysmile
happysmile

Reputation: 7777

repalcing the string value

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

Upvotes: 2

Views: 108

Answers (4)

tzaman
tzaman

Reputation: 47840

Console.WriteLine(Double.Parse("4.676767").ToString("0.##"));

Upvotes: 0

danish
danish

Reputation: 5600

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.

Upvotes: 0

brickner
brickner

Reputation: 6585

double value = double.Parse(str);
Console.WriteLine(value.ToString("##.##"));

Upvotes: 3

Related Questions