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. convert String to Decimal

convert String to Decimal

Scheduled Pinned Locked Moved C#
helptutorialquestion
17 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.
  • J jojoba2011

    Yes it returns : D= 192.0205000 BUT I wanna

    D= 192.0205  ====> Without unUsed Zeros
    
    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #4

    Either strip the zeros off the string or use Math.Round


    I know the language. I've read a book. - _Madmatt

    1 Reply Last reply
    0
    • J jojoba2011

      Hi codeproject World ! How can i convert string to decimal and remove unused Zero . example : string decimal = "192.0205000"; Decimal D = Decimal.Parse(decimal); and wanna to have : D = 192.0205 NOT D= 192.0205000 PLZ HELP !

      J Offline
      J Offline
      jojoba2011
      wrote on last edited by
      #5
      decimal d=  decimal.Parse("1120.00120000",System.Globalization.NumberStyles.AllowDecimalPoint);
      
      P P 2 Replies Last reply
      0
      • J jojoba2011
        decimal d=  decimal.Parse("1120.00120000",System.Globalization.NumberStyles.AllowDecimalPoint);
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #6

        Just a minor point - in numeric terms, 1120.00120000 is exactly the same as 1120.0012. If you're doing a calculation using a decimal then it doesn't make much sense to round trim it.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        A 1 Reply Last reply
        0
        • J jojoba2011

          Yes it returns : D= 192.0205000 BUT I wanna

          D= 192.0205  ====> Without unUsed Zeros
          
          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          JYIS, they aren't there! They don't exist! Edit: I posted that before I saw that you were using Decimal rather than Double. ToString: " The return value is formatted with the general numeric format specifier ("G"), and the NumberFormatInfo object for the current culture. " G: " The exception to the preceding rule is if the number is a Decimal and the precision specifier is omitted. In that case, fixed-point notation is always used and trailing zeroes are preserved. "

          modified on Wednesday, April 28, 2010 9:38 PM

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            Just a minor point - in numeric terms, 1120.00120000 is exactly the same as 1120.0012. If you're doing a calculation using a decimal then it doesn't make much sense to round trim it.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #8

            Indeed, though there is a difference in some situations, such as when using ToString(). Try running this:

            string strD = "192.0205000";
            Decimal D = Decimal.Parse(strD);
            MessageBox.Show(D.ToString());
            // Outputs: 192.0205000

            I never knew that the Decimal data type actually maintains the trailing zeroes. :doh:

            [Forum Guidelines]

            1 Reply Last reply
            0
            • J jojoba2011

              Hi codeproject World ! How can i convert string to decimal and remove unused Zero . example : string decimal = "192.0205000"; Decimal D = Decimal.Parse(decimal); and wanna to have : D = 192.0205 NOT D= 192.0205000 PLZ HELP !

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #9

              Excellent question! And thanks for providing the answer. I had no idea the Decimal data type stores trailing zeroes.

              jojoba2011 wrote:

              PLZ HELP !

              Just a bit of friendly advice... avoid using uppercase when not necessary, don't beg for help, and don't shorten words (e.g., "PLZ") except for common acronyms. To many people here at Code Project, it appears unprofessional. Other than that, though, great question. :thumbsup: EDIT: It appears your answer is incorrect.

              [Forum Guidelines]

              modified on Thursday, April 29, 2010 1:21 AM

              1 Reply Last reply
              0
              • P PIEBALDconsult

                JYIS, they aren't there! They don't exist! Edit: I posted that before I saw that you were using Decimal rather than Double. ToString: " The return value is formatted with the general numeric format specifier ("G"), and the NumberFormatInfo object for the current culture. " G: " The exception to the preceding rule is if the number is a Decimal and the precision specifier is omitted. In that case, fixed-point notation is always used and trailing zeroes are preserved. "

                modified on Wednesday, April 28, 2010 9:38 PM

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #10

                PIEBALDconsult wrote:

                JYIS

                What does that mean?

                PIEBALDconsult wrote:

                they aren't there! They don't exist!

                The trailing zeroes? Yes they do. See here.

                [Forum Guidelines]

                1 Reply Last reply
                0
                • J jojoba2011
                  decimal d=  decimal.Parse("1120.00120000",System.Globalization.NumberStyles.AllowDecimalPoint);
                  
                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #11

                  I see no difference.

                  1 Reply Last reply
                  0
                  • J jojoba2011

                    Hi codeproject World ! How can i convert string to decimal and remove unused Zero . example : string decimal = "192.0205000"; Decimal D = Decimal.Parse(decimal); and wanna to have : D = 192.0205 NOT D= 192.0205000 PLZ HELP !

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

                    This is merely a quirk of the "G" format; when ToStringing a Decimal, use something along the lines of d.ToString ( "#.###############" ).

                    A 1 Reply Last reply
                    0
                    • J jojoba2011

                      Hi codeproject World ! How can i convert string to decimal and remove unused Zero . example : string decimal = "192.0205000"; Decimal D = Decimal.Parse(decimal); and wanna to have : D = 192.0205 NOT D= 192.0205000 PLZ HELP !

                      I Offline
                      I Offline
                      Ice_Freez05
                      wrote on last edited by
                      #13

                      hello there... try this link...http://www.csharp-examples.net/string-format-double/[^] hope this help...

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        This is merely a quirk of the "G" format; when ToStringing a Decimal, use something along the lines of d.ToString ( "#.###############" ).

                        A Offline
                        A Offline
                        AspDotNetDev
                        wrote on last edited by
                        #14

                        Good call. One can also use:

                        d.ToString("G29");

                        [Forum Guidelines]

                        P 1 Reply Last reply
                        0
                        • A AspDotNetDev

                          Good call. One can also use:

                          d.ToString("G29");

                          [Forum Guidelines]

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

                          That's basically what the default is and I don't think it works.

                          A 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            That's basically what the default is and I don't think it works.

                            A Offline
                            A Offline
                            AspDotNetDev
                            wrote on last edited by
                            #16

                            Worked for me.

                            [Forum Guidelines]

                            1 Reply Last reply
                            0
                            • J jojoba2011

                              Hi codeproject World ! How can i convert string to decimal and remove unused Zero . example : string decimal = "192.0205000"; Decimal D = Decimal.Parse(decimal); and wanna to have : D = 192.0205 NOT D= 192.0205000 PLZ HELP !

                              A Offline
                              A Offline
                              AspDotNetDev
                              wrote on last edited by
                              #17

                              After reading what everybody has said, I think you want this:

                              Decimal d = Decimal.Parse(Decimal.Parse("192.0205000").ToString("G29"));

                              [Forum Guidelines]

                              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