468,554 Members | 1,056 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

Help with String.Format

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

Nov 17 '05 #1
7 6248
KH
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" wrote:
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);

Nov 17 '05 #2
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" wrote:
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" wrote:
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);

Nov 17 '05 #3
Alpha wrote:
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?


http://msdn.microsoft.com/library/en...matstrings.asp
--
Chris Priede (pr****@panix.com)
Nov 17 '05 #4
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

"Chris Priede" wrote:
Alpha wrote:
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?


http://msdn.microsoft.com/library/en...matstrings.asp
--
Chris Priede (pr****@panix.com)

Nov 17 '05 #5
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);

Nov 17 '05 #6
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" wrote:
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);


Nov 17 '05 #7
Greetings Alpha!

"Alpha" wrote:
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
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by cg_news | last post: by
22 posts views Thread by KitKat | last post: by
1 post views Thread by sallyk57 | last post: by
8 posts views Thread by Lucky | last post: by
9 posts views Thread by pic078 via AccessMonster.com | last post: by
5 posts views Thread by jrod11 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.