Need Help format String for a Number The Old "\f" Style?

  • Thread starter Thread starter NeilGott
  • Start date Start date
N

NeilGott

I am trying to format a decimal number such as 0.0567
but forgot how to do formatting in C#.

I tried the old C format escape sequence "\8.3f" the old C style but does
not work.
example: dMyNumber.tostring("\8.3f");

Can some one refresh how to do this?

Thanks
 
NeilGott said:
I am trying to format a decimal number such as 0.0567
but forgot how to do formatting in C#.

I tried the old C format escape sequence "\8.3f" the old C style but does
not work.
example: dMyNumber.tostring("\8.3f");

Can some one refresh how to do this?

Thanks
double doubleNumber;
doubleNumber = 18934.1879;
Console.WriteLine(doubleNumber.ToString("F",
CultureInfo.InvariantCulture));
 
Thanks for your reponse. So how come the old C style formats do not work?

Do you know how I can specify precision, say I want 5 decimal places etc?
 
Thanks for the link Jeff. So the old escape foramt type in Standard C,
i.e %8.5f or "%6.3d" do not work with ToString().

I seem to recall being able to do this in Csharpe.

Would use a few examples, here is what I use to do in old c style:
fprintf(' Mean Of Cumulative Weighted Returns %8.3f\n', MeanCumulative)

Hope you can give me a few examples, so rusty on formatting!

Thanks.
 
NeilGott said:
So how come the old C style formats do not work?
For much the same reason there's no printf(): apart from the syntactical
resemblance, C# and C are completely different languages. On the other hand,
the same formatting strings work in C# and VB.NET, because they're both .NET
languages and they share the same framework library.
 
Jeroen:

I tried the old formating strings (i.e dMNumber.tostring("%8.3f")
and it does not work. Seems the 83 above gets appedned to the number.

So old style doesn't seem to work with .NET tostring() function.

Perhaps i am wrong?
 
NeilGott said:
I am trying to format a decimal number such as 0.0567
but forgot how to do formatting in C#.

I tried the old C format escape sequence "\8.3f" the old C style but does
not work.
example: dMyNumber.tostring("\8.3f");

Can some one refresh how to do this?

Thanks

I think that the composite formatting string "f3,8" is what you are
looking for:

string formatted = String.Format("{0:f3,8}", theNumber);

"f3" formats the number rounded to three decimal places, and ",8" right
aligns it to the length of eight characters.
 
NeilGott said:
I tried the old formating strings (i.e dMNumber.tostring("%8.3f")
and it does not work. Seems the 83 above gets appedned to the number.

So old style doesn't seem to work with .NET tostring() function.

Perhaps i am wrong?
No, you're not. Where did I ever say this should work? What I said was that
C# and VB.NET (and all the other .NET languages) support the same formatting
strings, which are explicitly *not* C formatting strings.
 
Thanks for your response.
I must be doing something wrong trying your suggestion,
it prints f38 when I tried this:

txtbResults.AppendText("New Ratio Simulated: " +

String.Format("{0:f3,8}",dSimulatedRatioData))+ Environment.NewLine);

documentation online just does not show the old K&R C style formating

from an old K&R C programmer.

Anyone know how to apply this correcty?
 
NeilGott said:
Thanks for your response.
I must be doing something wrong trying your suggestion,
it prints f38 when I tried this:

txtbResults.AppendText("New Ratio Simulated: " +

String.Format("{0:f3,8}",dSimulatedRatioData))+ Environment.NewLine);

documentation online just does not show the old K&R C style formating

from an old K&R C programmer.

Anyone know how to apply this correcty?

I see... I just got the order of the composites wrong. The alignment
goes after the index, not after the format string:

String.Format("{0,8:f3}", dSimulatedRatioData)

Here's the reference:
http://msdn.microsoft.com/en-us/library/txafckwd.aspx
 
Goran:

Thanks worked. I probably did this over year ago an totally forgot.
The old use it or lose it situation of keeping up with programming.

Thanks Again
 
Back
Top