format number #,### doesn't work right

D

D. Patrick

When I use
String.Format("{0:#,###}", intNumber)
it works fine until intNumber = 0, and then it returns an empty string!

I just want to show all numbers with commas in thousands place. Is there a
better way? I tried "0,0", but that returns "00" when the integer is 0.
 
D

David Hogue

D. Patrick said:
When I use
String.Format("{0:#,###}", intNumber)
it works fine until intNumber = 0, and then it returns an empty string!

I just want to show all numbers with commas in thousands place. Is there a
better way? I tried "0,0", but that returns "00" when the integer is 0.

You're close "#,0" worked for me. So did "#,##0" and as far as I can
tell they are the same.

BTW does anyone out there have a good list of all the different format
strings? I wasn't able to find much on MSDN. Info seems a bit scarce
in this area. Below are a few links I found:

http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx
http://blog.stevex.net/index.php/string-formatting-in-csharp/
 
G

Guest

it do not support your format about ","

example

int intNumber=0 ;
if(intNumber ==0)
{
MessageBox.Show("0");
}
else
{
MessageBox.Show(String.Format("{0:#,###}", intNumber));
}

cheers
 
D

D. Patrick

I appreciate the post, but do you mean this is really by design? And people
must always test for 0 separately? That seems like a mistake.
 
J

Jan Hyde

"D. Patrick" <[email protected]>'s wild
thoughts were released on Wed, 14 Jun 2006 03:41:36 GMT
bearing the following fruit:
When I use
String.Format("{0:#,###}", intNumber)
it works fine until intNumber = 0, and then it returns an empty string!

That is the correct and documented behaviour.
I just want to show all numbers with commas in thousands place. Is there a
better way? I tried "0,0", but that returns "00" when the integer is 0.

You can specifically tell it what you want it to do when the
value is 0

String.Format("{0:#,###;;zero}", intNumber)

or simply

String.Format("{0:#,##0",intNumber)





Jan Hyde (VB MVP)
 
Joined
Mar 22, 2011
Messages
1
Reaction score
0
I used the NumberTool from VelocityTools

Strangely, the documentation for velocity tools is out of date, and the args/params in the documentation is a little different than how it actually works. This is working for me in the Velocity layer in a .vm template file:

$number.format('integer', 100000) ## outputs 100,000

Notice the documentation here is outdated, using the incorrect syntax $number.integer...

http://velocity.apache.org/tools/re...apache/velocity/tools/generic/NumberTool.html

Also notice that in toolbox.xml, I do not have additional params for format as documented in the link above. Instead, I simply have:

<tool>
<key>number</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.NumberTool</class>
</tool>
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top