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. General Programming
  3. C#
  4. Only show decimals if there are any

Only show decimals if there are any

Scheduled Pinned Locked Moved C#
databasealgorithmsquestion
10 Posts 7 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.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.

    E W G P D 5 Replies Last reply
    0
    • E Etienne_123

      Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.

      E Offline
      E Offline
      Etienne_123
      wrote on last edited by
      #2

      So I found a way of doing this. If you use Decimal.ToDouble it seems to do the job :)

      L 1 Reply Last reply
      0
      • E Etienne_123

        Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        I am not sure which version of .Net you are using but in VS 2010(.Net 4.0) in a winforms app, this seems to be the default behaviour. I know if I want to always show the decimal part (500.00), that is when I need to go the ToString route. I just tested with a listbox and with textboxes, and I get no decimal points if the decimal is a whole number.

        ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

        1 Reply Last reply
        0
        • E Etienne_123

          Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...

          Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
          W B E 3 Replies Last reply
          0
          • G GuyThiebaut

            From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...

            Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
            W Offline
            W Offline
            Wayne Gaylard
            wrote on last edited by
            #5

            Hear, Hear.

            ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

            1 Reply Last reply
            0
            • G GuyThiebaut

              From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...

              Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              I agree with you.


              I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com

              1 Reply Last reply
              0
              • E Etienne_123

                So I found a way of doing this. If you use Decimal.ToDouble it seems to do the job :)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Converting your Decimal value to Double may result in loss of precision. Instead, keep it Deciamal and use ToString() to display it in a certain format.

                1 Reply Last reply
                0
                • E Etienne_123

                  Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Etienne_123 wrote:

                  ONLY show decimals

                  Show it where? :confused:

                  1 Reply Last reply
                  0
                  • G GuyThiebaut

                    From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...

                    Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
                    E Offline
                    E Offline
                    Etienne_123
                    wrote on last edited by
                    #9

                    I`m not too concerned with precision. At the moment it works perfectly - If I do have decimals it keeps the decimals and if I don't then it shows the whole number. I just tested this by creating a random decimal of 4.4567, converted it to a double and the value stayed the same

                    1 Reply Last reply
                    0
                    • E Etienne_123

                      Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #10

                      Etienne_123 wrote:

                      I do not want to use string formattin

                      What is displayed on screen is a string that is currently using the default formatting, so doing a custom string format is the correct way. The sort should be done on the real values regardless of how it's displayed IMO, this way therer is no issue.

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      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