[英]Format string for decimal output
I just started programming C# today, and I'm trying to get a handle on the format strings. 我今天刚开始编写C#,我正在尝试处理格式字符串。
I would like to output a string of this format: 我想输出这种格式的字符串:
{decimal number using a maximum of 10 columns} +
" " +
{decimal number taking a maximum of 10 columns}
I've tried 我试过了
String outputFormat = "{0,-10:0.#####} {1,-10:0.#####}";
String output = String.Format(outputFormat, x, y);
But if x
is 34059834.340598
, I don't get the output I want. 但如果x
是34059834.340598
,我没有得到我想要的输出。
The first number takes more than 10 columns. 第一个数字超过10列。
Is there a format string to force numbers to 10 columns? 是否有格式字符串强制数字为10列? (Maybe show E notation if the nm would be greater than 10 columns). (如果nm大于10列,可能会显示E表示法)。
I think you are looking for the G specifier for you number formating. 我认为您正在为数字格式化寻找G说明符 。
Something like this (untested) should work: 这样的事情(未经测试) 应该有效:
string tenCols = myDecimal.ToString("G10");
Or to be more inline with what you had before, I think this should do it: 或者为了更加符合你以前所拥有的内容,我认为应该这样做:
String outputFormat = "{0,-10:G10} {1,-10:G10}";
String output = String.Format(outputFormat, x, y);
double x = 34059834.340598
string displayX = x.ToString().Substring(0,10);
I'm pretty sure you won't be able to do what you want using format strings directly, but it's easy enough with string manipulation in code. 我很确定你不能直接使用格式字符串来做你想做的事情,但是在代码中使用字符串操作很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.