我需要将十进制值格式化为 8 个位置,其中最后 2 位数字保留用于十进制值。
格式应为:00000000(最后 2 个零表示十进制值)。
例子:
十进制值:193.45
The result should be: 0000019345
十进制值:245
The result should be: 0000024500
我知道我可以使用 string.format("{format here}", value)
或 .ToString("format here")
但不知道什么字符串要使用的格式。
最佳答案
查看 Custom Numeric Format Strings 的 MSDN 文档.
很可能定义一个自定义 NumberFormatInfo以这种格式打印字符串。但是,实现此目的的更简单方法是以下任一选项:
(value * 100).ToString("00000000");
string.Format("{0:00000000}", value * 100);
关于c# - 如何格式化带前导零且不带分隔符的小数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26069918/