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

Discussion in 'ASP .Net' started by D. Patrick, Jun 14, 2006.

  1. D. Patrick

    D. Patrick Guest

    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. Patrick, Jun 14, 2006
    #1
    1. Advertising

  2. D. Patrick

    David Hogue Guest

    D. Patrick wrote:
    > 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/

    --
    David Hogue
     
    David Hogue, Jun 14, 2006
    #2
    1. Advertising

  3. 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. Patrick" wrote:

    > 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.
    >
    >
    >
     
    =?Utf-8?B?am9uZ3dvbw==?=, Jun 14, 2006
    #3
  4. D. Patrick

    D. Patrick Guest

    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.

    "jongwoo" <> wrote in message
    news:...
    >
    > 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. Patrick" wrote:
    >
    >> 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. Patrick, Jun 14, 2006
    #4
  5. D. Patrick

    Jan Hyde Guest

    "D. Patrick" <>'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)

    --
    Incest is relatively boring. Necrophillia is dead boring. (Hugh Janus)
     
    Jan Hyde, Jun 14, 2006
    #5
  6. D. Patrick

    shogo2040

    Joined:
    Mar 22, 2011
    Messages:
    1
    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>
     
    shogo2040, Mar 22, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    14,555
  2. Protoman

    Encryption doesn't work right

    Protoman, Oct 2, 2005, in forum: C++
    Replies:
    6
    Views:
    336
  3. Randy Kramer
    Replies:
    2
    Views:
    421
    Randy Kramer
    Jan 12, 2007
  4. George Hester

    onload doesn't work right with applets in a page

    George Hester, Jan 13, 2004, in forum: Javascript
    Replies:
    2
    Views:
    273
    George Hester
    Jan 17, 2004
  5. Fei Liu
    Replies:
    21
    Views:
    2,464
    John Bokma
    Dec 16, 2006
Loading...

Share This Page