473,411 Members | 2,030 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,411 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 6471
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.