开发者

formatting double precision values in C#

开发者 https://www.devze.com 2022-12-17 05:45 出处:网络
Say I have a double variable initialized as double dValue= 5.156365 I would like to show this in a textbox as 5.16 i.e only two decimal places.

Say I have a double variable initialized as

double dValue  = 5.156365

I would like to show this in a textbox as 5.16 i.e only two decimal places.

How should I format?

Is textbox.Text = dValue.ToString("F2", Culture.....) correct? When I tried it did give me the correct result. However, if dValue = 5 then I would like only 开发者_StackOverflow5 to be shown and not 5.00.

How could I achieve this in C#?


A 0 in the string forces that decimal place, while a # lets the number get up to that decimal place.

   dValue.ToString("0.##")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号