【发布时间】:2014-10-01 04:30:46
【问题描述】:
我想使用 String.Format 指定小数位数以显示小数位数在运行时可能发生变化的位置。
通常我会这样做:
Dim d As Decimal = 1.23456D
Debug.WriteLine(String.Format("My number to 2 decimal places is {0:f2}", d))
我想做的是这样的:
Dim d As Decimal = 1.23456D
Dim places as integer = 2
Debug.WriteLine(String.Format("My number to {0} decimal places is {1:f" + places.ToString + "}", places, d))
但不必连接字符串。有没有办法做到这一点?
【问题讨论】:
-
据我所知,没有。我能想到的最好的办法是嵌套你的
string.Format调用,这会很糟糕。不过,我很想将格式提取到您传递的格式中:string.Format("...places is {1}", places, d.ToString("f" + places));。我知道,它们每一种都很恶心,但至少你正在处理一个常量格式字符串。
标签: .net vb.net precision numeric string.format