Hi Guys
NP136 what is the meaning of it?
In the following program elements in the bracket of String.Format() method (highlighted in yellow) can be unerstood as follows:
0 – Position of the variable
f – Floating point
3 – Number of digits
10 – What is the meaning of it?
Please explain the reason.
Thank you
using System;
class MainClass
{
public static void Main()
{
float myFloat = 1234.56789f;
string myString8 = String.Format("{0, 10:f3}", myFloat);
Console.WriteLine("String.Format(\"{0, 10:f3}\", myFloat) = " + myString8);
}
}
/*
String.Format("{0, 10:f3}", myFloat) = 1234.568
*/
AlanPosted Aug 25, 2008, 3:47 PM
Looking at the result of running this program:
/*
String.Format("{0, 10:f3}", myFloat) = 1234.568
*/
you'll notice that the position of the number 1234.568 is offset two spaces to the right.
This is because it's been placed inside a buffer (technically known as a 'field') which is 10 characters wide and is aligned to the right hand side of that buffer ('right justified'). The number itself is 8 characters wide and so the first two places in the buffer are filled with spaces.
This is a useful feature if you wish to output several numbers to the console and align their final digits so they all appear in the same column.
Posted Aug 25, 2008, 7:27 PM
Thank you very much for explanation.
AlanPosted Aug 25, 2008, 7:16 PM
'Justification' is a term originally used in typesetting (of books, newspapers etc) which is now used in the manipulation of text generally by computers or other means.
Text is said to be justified if each line is spaced out so it ends at the right margin of the page. Otherwise it's called 'ragged'.
The term probably derives from the usual meaning of justify (to prove right).
Nowadays, we talk about both right and left justification depending on whether the text is aligned on the right or left margins.
Posted Aug 25, 2008, 6:03 PM
Thank you for above explanation.
Why do it is called right justified & left justified?
AlanPosted Aug 25, 2008, 5:50 PM
Well, in the second line, the numbers are left justified within a buffer of 5 characters and, in the third line, within a buffer of 10 characters.
Notice also that there's a further space between the numbers (i.e. between the two sets of {}). So, these lines come out as:
123xx 456xx
000123xxxx 000456
where 'x' denotes a space padding put the buffer (not actually visible, of course).
Posted Aug 25, 2008, 4:17 PM
Thank you for above explanation.
In the following program how can left justification be explained?
using System;
public class TestConsoleApp
{
public static void Main(string[] args)
{
Console.WriteLine("{0,5} {1,5}", 123, 456); // Right-aligned
Console.WriteLine("{0,-5} {1,-5}", 123, 456); // Left-aligned
Console.WriteLine("{0,-10:D6} {1,-10:D6}", 123, 456);
}
}
/*
123 456
123 456
000123 000456
*/
Posted Aug 25, 2008, 3:27 PM
It is much appreciated if you could elaborate the above answer.
AlanPosted Aug 25, 2008, 10:27 AM
The 10 means that the number to be formatted is right justified within a field 10 characters long.
If it had been -10 it would have been left justified in such a field.