473,224 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 6462
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cg_news | last post by:
In short, what I am trying to do is, based on a date, calculate the week of year (as described in ISO 8601), then calculate the first and last date in this week period and return them in the format...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
4
by: Anon | last post by:
Hello All! I have written a simple app that auto downloads a file from a secure ftp, renames it, and moves it to a network location. Everything works great except the renaming part. I parse out...
22
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
1
by: sallyk57 | last post by:
Help with following Programs: Write two programs one where the performance rating here shoud be entered as a int where Excellent =1, Good= 2, Poor=3. an employee who is rated excellent will...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.