Hi Alpha!
First of all, let me tell you my commiseration for having to maintain code
as bad as the one you posted. Your predecessor didn't do you a favour at
all.
To find information about format strings you should search in the MSDN. Try
the following
http://msdn.microsoft.com/library/de...matstrings.asp
and read the documentation of the String.Format method.
The format string {0:.#0}means that the first parameter in the parameter
list is formatted with format string ".#0".
..#0 says that exactly two digits after the decimal point should be
formatted. That no digits before the decimal point should be formatted can
be seen because there is no format specifyer before the poin.
0 is a placeholder for an arbitrary digit
# is a placeholder for an arbitrary digit if it is significant. However,
because it stands behind the decimal point and there is a "0" after the "#"
it always is significant.
Normally one would simply use an expression as the following to achieve the
formatting you desire:
string.Format("{0,12:#,##0.00}", amountAsDec);
The 0,12 says that the first parameter of the parameter list should be
formatted to have an overall length of 12 characters. The rest #,##0.00 says
that the thousand-separator should separate the formatted number in blocks
of three digits and that there should be two digits after the decimal point.
However, this format string would use a "." as decimal separator and it
seems that most of the code you posted is just about changing the point to a
blank character. However, as the code relies on specific formatting
behaviour you might run into troubles if running your code with different
format providers, say because it's run in a different culture. Maybe you
should try something like the following:
decimal amountAsDec = drTrips.Row["Amount"];
System.Globalization.NumberFormatInfo
spaceSeparatedFormatProvider =
System.Threading.Thread.CurrentThread.CurrentCultu re.NumberFormat.Clone()
as System.Globalization.NumberFormatInfo;
spaceSeparatedFormatProvider .NumberDecimalSeparator = " ";
string amountAsStr = string.Format(spaceSeparatedFormatProvider,
"{0,12:#,##0.00}", amountAsDec);
Hope that helps!
- Markus
"Alpha" <Al***@discussions.microsoft.com> schrieb im Newsbeitrag
news:8A**********************************@microsof t.com...
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm
looking for codes that's droping the 2nd digit of a nuber printed out and
I
suspect it's the code below. Can someone tell me where I can look up
explaination on the String.Format, the format string part like
"("{0:.#0}".
What's the trailing 0 and what is that "0:" means? I assume the "#" is
the
place holder character for th value obtain after the comma in the code.
Thanks, Alpha
string amountdecimal =
String.Format("{0:.#0}",Convert.ToDecimal(drTrips. Row["Amount"]) - (int)
Convert.ToDecimal(drTrips.Row["Amount"]));
if(amountdecimal.StartsWith("-"))
{
amountdecimal = amountdecimal.Substring(1,amountdecimal.Length-1);
}
amount = String.Format("{0,9:####,##0}",(int)
Convert.ToDecimal(drTrips.Row["Amount"])) + " " +
amountdecimal.Substring(1,2);