Help with String.Format

G

Guest

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);
 
G

Guest

Look up NumberFormatInfo class in the SDK ... you can also find number
formatting info and links in the .ToString() method overloads of numeric
types.
 
G

Guest

I have been reading the MSDN libraray on these subjects but feels like I'm
just going around in a loop. I'm still lookin for specifics and definitions
on how to compose the format string. There are a lot of definition on
Iformat....... But I haven't found any specific that's pertain to my
quesiton.

Thanks, Alpha

KH said:
Look up NumberFormatInfo class in the SDK ... you can also find number
formatting info and links in the .ToString() method overloads of numeric
types.


Alpha said:
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);
 
G

Guest

Thank you, thank you, thank you. I have been looking for this on MSDN
forever. I found my answer in the page you provided and cliked the link to
the "Custom Numberic Format". Finally, the answers are there. There are so
much definition and information so sometimes it's hard to find the specifics.
Thanks you so much again,
Alpha
 
M

Markus Minichmayr

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/d...uide/html/cpconcustomnumericformatstrings.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.CurrentCulture.NumberFormat.Clone()
as System.Globalization.NumberFormatInfo;

spaceSeparatedFormatProvider .NumberDecimalSeparator = " ";
string amountAsStr = string.Format(spaceSeparatedFormatProvider,
"{0,12:#,##0.00}", amountAsDec);

Hope that helps!
- Markus



Alpha said:
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);
 
G

Guest

Thank you so much for taking the time to share your knowledge. This is most
helpful. Now, it makes a lot of sense. Your code is definitely the best way
to do this.
Cheers,
Alpha
Markus Minichmayr said:
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/d...uide/html/cpconcustomnumericformatstrings.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.CurrentCulture.NumberFormat.Clone()
as System.Globalization.NumberFormatInfo;

spaceSeparatedFormatProvider .NumberDecimalSeparator = " ";
string amountAsStr = string.Format(spaceSeparatedFormatProvider,
"{0,12:#,##0.00}", amountAsDec);

Hope that helps!
- Markus



Alpha said:
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);
 
G

Guest

Greetings Alpha!

Alpha said:
I have been reading the MSDN libraray on these subjects but feels like I'm
just going around in a loop.

I've just had the same experience! I wanted to print a number as hex. I
already knew about WriteLine("{0}",myNumber), but I just went round in
circles trying to find out how to do it in hex. Sure, MSDN told me to use an
"h", but after trying {0h}, %h{0}, etc..etc.. I just gave up..eventually I
found the answer in this forum..{0:h}. The format doco has all the detail,
but is hopeless when you're getting started. It reminds my Unix man pages!!!

in sympathy,

Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top