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 a decimal number

format a decimal number

Scheduled Pinned Locked Moved ASP.NET
questiontutorial
8 Posts 4 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.
  • C Offline
    C Offline
    coolpali_girl
    wrote on last edited by
    #1

    Hi, How do i specify a decimal number to take only two numbers after the "."? for example: if i have this number in a lable 23.222342114, how do i write my cobe to be displayed only like this 23.22 ?? thanks,

    W 1 Reply Last reply
    0
    • C coolpali_girl

      Hi, How do i specify a decimal number to take only two numbers after the "."? for example: if i have this number in a lable 23.222342114, how do i write my cobe to be displayed only like this 23.22 ?? thanks,

      W Offline
      W Offline
      Wesley Samuel
      wrote on last edited by
      #2

      object.ToString ( "Fn" ) where F is a single alphabetic character called the format specifier, and n can be one or more digits called the precision specifier. Format specifier for decimal D or d so you have to use object.ToString("D2");

      C 1 Reply Last reply
      0
      • W Wesley Samuel

        object.ToString ( "Fn" ) where F is a single alphabetic character called the format specifier, and n can be one or more digits called the precision specifier. Format specifier for decimal D or d so you have to use object.ToString("D2");

        C Offline
        C Offline
        coolpali_girl
        wrote on last edited by
        #3

        I tried what you said but it's not accepting the D2..i'm using a vb.net code so are you sure it's right?

        V 1 Reply Last reply
        0
        • C coolpali_girl

          I tried what you said but it's not accepting the D2..i'm using a vb.net code so are you sure it's right?

          V Offline
          V Offline
          Vipin Venugopal
          wrote on last edited by
          #4

          i will just show one example ok decimal i=123.23444; string j=Convert.ToString(Math(i,2)); if print j u will be getting 123.23 Hope this works for u.

          C 1 Reply Last reply
          0
          • V Vipin Venugopal

            i will just show one example ok decimal i=123.23444; string j=Convert.ToString(Math(i,2)); if print j u will be getting 123.23 Hope this works for u.

            C Offline
            C Offline
            coolpali_girl
            wrote on last edited by
            #5

            no, it's not working :( i think what you wrote is a c# code not a vb.net when i wrote your code it displayed an error under the word "Math" !

            _ V 2 Replies Last reply
            0
            • C coolpali_girl

              no, it's not working :( i think what you wrote is a c# code not a vb.net when i wrote your code it displayed an error under the word "Math" !

              _ Offline
              _ Offline
              _AK_
              wrote on last edited by
              #6

              you can simply use this to get the desired output. string str = Convert.ToString(Math.Round(i,2)); here i is your decimal number.

              Best Regards, Apurva Kaushal

              1 Reply Last reply
              0
              • C coolpali_girl

                no, it's not working :( i think what you wrote is a c# code not a vb.net when i wrote your code it displayed an error under the word "Math" !

                V Offline
                V Offline
                Vipin Venugopal
                wrote on last edited by
                #7

                Iam really sorry for the mistake. actually i missed one small piece of code. Decimal i=Convert.ToDecimal(textBox1.Text); textBox2.Text=Convert.ToString(Math.Round(i,2)); u should give Math.Round(i,2) where i is the decimal no and 2 is the no of decimal places to be rounded to. this will surely work. I am sorry for the previous error ok. Vipin

                C 1 Reply Last reply
                0
                • V Vipin Venugopal

                  Iam really sorry for the mistake. actually i missed one small piece of code. Decimal i=Convert.ToDecimal(textBox1.Text); textBox2.Text=Convert.ToString(Math.Round(i,2)); u should give Math.Round(i,2) where i is the decimal no and 2 is the no of decimal places to be rounded to. this will surely work. I am sorry for the previous error ok. Vipin

                  C Offline
                  C Offline
                  coolpali_girl
                  wrote on last edited by
                  #8

                  Thanks guys it's working now :D

                  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