String.format with numbers less than zero

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hharry

    String.format with numbers less than zero

    Hello All,

    I am using the following to format numbers precise to 2 decimal places
    with thousand separators:

    Double number = 100000.87;
    string MyString = String.Format(" {0:0,0.00}", number);
    // 100,000.87
    which is fine.

    When the number is less than zero:
    Double number = 0.8756;
    string MyString = String.Format(" {0:0,0.00}", number);
    // 00.87
    How do output 0.87 instead of 00.87 ?

    Thanks in advance.



  • Alcides

    #2
    Re: String.format with numbers less than zero

    Can try to use String.Format(" {0:#,0.00}", number)
    The "# " character will do it.

    Alcides Schulz
    Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

    Comment

    • Claes Bergefall

      #3
      Re: String.format with numbers less than zero

      string MyString = number.ToString ("N2");

      /claes

      "hharry" <paulquigley@ny c.comwrote in message
      news:e9576758-09cf-4d1f-9315-5bcb37939053@p2 5g2000hsf.googl egroups.com...
      Hello All,
      >
      I am using the following to format numbers precise to 2 decimal places
      with thousand separators:
      >
      Double number = 100000.87;
      string MyString = String.Format(" {0:0,0.00}", number);
      // 100,000.87
      which is fine.
      >
      When the number is less than zero:
      Double number = 0.8756;
      string MyString = String.Format(" {0:0,0.00}", number);
      // 00.87
      How do output 0.87 instead of 00.87 ?
      >
      Thanks in advance.
      >
      >
      >

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: String.format with numbers less than zero


        "hharry" <paulquigley@ny c.comwrote in message
        news:e9576758-09cf-4d1f-9315-5bcb37939053@p2 5g2000hsf.googl egroups.com...
        Hello All,
        >
        I am using the following to format numbers precise to 2 decimal places
        with thousand separators:
        >
        Double number = 100000.87;
        string MyString = String.Format(" {0:0,0.00}", number);
        // 100,000.87
        which is fine.
        >
        When the number is less than zero:
        Double number = 0.8756;
        string MyString = String.Format(" {0:0,0.00}", number);
        // 00.87
        How do output 0.87 instead of 00.87 ?
        >
        Thanks in advance.
        Would you mind explaining when 0.8756 became less than zero?

        Comment

        Working...