1

I just started programming C# today, and I'm trying to get a handle on the format strings.

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.

The first number takes more than 10 columns.

Is there a format string to force numbers to 10 columns? (Maybe show E notation if the n.m would be greater than 10 columns).

2
  • What do you mean bu "Columns"? What is the output you want from 34059834.340598 ? Commented Sep 27, 2012 at 17:14
  • I dont understand what you want. Can you show the inputs and the output you want? Commented Sep 27, 2012 at 17:18

2 Answers 2

3

I think you are looking for the G specifier for you number formating.

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);
Sign up to request clarification or add additional context in comments.

Comments

0
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.

3 Comments

What if x was 100000000000000000.581. I don't want to lose information, just precision.
So if x was 100000000000000000.234 then you would want 1000000000 0000000023 then? 10 digits, space, 10 digits? That seems weird. Why not just always do scientific (E) notation?
I want scientific notation if the decimal number would be larger than 10 digits, else I want standard notation. String.Format() does this by default when the floating point value has lost precision on your hardware.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.