Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Format String for Decimal {0:D2}

Format String for Decimal {0:D2}

Scheduled Pinned Locked Moved ASP.NET
dockerquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OldDog Net
    wrote on last edited by
    #1

    Hi, I've been trying to format some fields as Decimal numbers but not as currency. The other numeric formats work great (currency, scientific notation, etc.) and the "D" works fine with Dates. The "D" will not work for me on a numeric field though. I've tried both embedding in the DataBinder statement: <%# DataBinder.Eval(Container, "DataItem.AmountPaid", "{0:D2}") %> and as a DataFormatString: DataFormatString="{0:D2}" I've also tried the ItemDataBound event. No luck. Any suggestions? Will

    J G 2 Replies Last reply
    0
    • O OldDog Net

      Hi, I've been trying to format some fields as Decimal numbers but not as currency. The other numeric formats work great (currency, scientific notation, etc.) and the "D" works fine with Dates. The "D" will not work for me on a numeric field though. I've tried both embedding in the DataBinder statement: <%# DataBinder.Eval(Container, "DataItem.AmountPaid", "{0:D2}") %> and as a DataFormatString: DataFormatString="{0:D2}" I've also tried the ItemDataBound event. No luck. Any suggestions? Will

      J Offline
      J Offline
      Jon Sagara
      wrote on last edited by
      #2

      Are you just trying to display two decimal places? If so, use the F format specifier: Decimal pi = new Decimal (Math.PI); Console.WriteLine ("{0:F2}", pi);

      Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

      O 1 Reply Last reply
      0
      • O OldDog Net

        Hi, I've been trying to format some fields as Decimal numbers but not as currency. The other numeric formats work great (currency, scientific notation, etc.) and the "D" works fine with Dates. The "D" will not work for me on a numeric field though. I've tried both embedding in the DataBinder statement: <%# DataBinder.Eval(Container, "DataItem.AmountPaid", "{0:D2}") %> and as a DataFormatString: DataFormatString="{0:D2}" I've also tried the ItemDataBound event. No luck. Any suggestions? Will

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        What is the data type of the field? The "D" specifier will only work with integral types.

        --- b { font-weight: normal; }

        O 1 Reply Last reply
        0
        • J Jon Sagara

          Are you just trying to display two decimal places? If so, use the F format specifier: Decimal pi = new Decimal (Math.PI); Console.WriteLine ("{0:F2}", pi);

          Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

          O Offline
          O Offline
          OldDog Net
          wrote on last edited by
          #4

          Thanks! That's doing it for me. I ignored the "F" because description in the MSDN doc is pretty vague (fixed) . . . until you follow the chain to the "Standard Numeric Format Strings". I just didn't go far enough the first time. I also see there that the "D" only works with integral types as the other poster suggested. I'm not sure what type results from the DataBinder statement? Evidently it's not a String or Decimal. Thanks again.

          1 Reply Last reply
          0
          • G Guffa

            What is the data type of the field? The "D" specifier will only work with integral types.

            --- b { font-weight: normal; }

            O Offline
            O Offline
            OldDog Net
            wrote on last edited by
            #5

            I'm not sure what type results from the DataBinder statement? Evidently it's not a String or Decimal. But, I see what you mean now that I found "Standard Numeric Format Strings" item in the MSDN doc. I circumvented the issue by using the Fixed format instead. Some time when I'm not under the gun I'll have to follow up and figure this one out. Thanks.

            G 1 Reply Last reply
            0
            • O OldDog Net

              I'm not sure what type results from the DataBinder statement? Evidently it's not a String or Decimal. But, I see what you mean now that I found "Standard Numeric Format Strings" item in the MSDN doc. I circumvented the issue by using the Fixed format instead. Some time when I'm not under the gun I'll have to follow up and figure this one out. Thanks.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              The DataBinder returns a value of the type Object, but the real type of the value depends on the data type of the source. If you for example have a int field in the data base, the object contains an Int32 value.

              --- b { font-weight: normal; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups