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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Convert String to decimal

Convert String to decimal

Scheduled Pinned Locked Moved C#
9 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
    cppdotnet
    wrote on last edited by
    #1

    Hi I have a decimal value which i want to convert to string but it drops off .0 (decimal) I can't format since whatever value in decimal I want it as it is in string ex. 88.99 88.00 88.0 or just 88 Thanks Thanks

    J 1 Reply Last reply
    0
    • C cppdotnet

      Hi I have a decimal value which i want to convert to string but it drops off .0 (decimal) I can't format since whatever value in decimal I want it as it is in string ex. 88.99 88.00 88.0 or just 88 Thanks Thanks

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      double val = 88.00;
      string valStringWithZeroes = string.Format("{0:0.00}", val);

      You can see a list of string formatting options from this blog posting[^].

      C 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        double val = 88.00;
        string valStringWithZeroes = string.Format("{0:0.00}", val);

        You can see a list of string formatting options from this blog posting[^].

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

        This is great but doesn't work if i have 88.0 it will format that to 88.00 I want which shows # as it is ... like 88.0 or 88888.888 Thanks

        J Q 2 Replies Last reply
        0
        • C cppdotnet

          This is great but doesn't work if i have 88.0 it will format that to 88.00 I want which shows # as it is ... like 88.0 or 88888.888 Thanks

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Your problem is that double val = 88.0; Is the same value as double val = 88.0000; They're the same in the real mathematical sense of decimal values, as well as the same in the CLR. So, there's no way for you to print out whether it's 88.0 or 88.0000 if you're storing it as a double. I'm not sure whether there's a type in the CLR that stores an arbitrary number of decimal places as part of the value type itself.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

          D 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Your problem is that double val = 88.0; Is the same value as double val = 88.0000; They're the same in the real mathematical sense of decimal values, as well as the same in the CLR. So, there's no way for you to print out whether it's 88.0 or 88.0000 if you're storing it as a double. I'm not sure whether there's a type in the CLR that stores an arbitrary number of decimal places as part of the value type itself.

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #5

            Judah Himango wrote:

            They're the same in the real mathematical sense of decimal values, as well as the same in the CLR. So, there's no way for you to print out whether it's 88.0 or 88.0000 if you're storing it as a double. I'm not sure whether there's a type in the CLR that stores an arbitrary number of decimal places as part of the value type itself.

            For something not being modifed in code, String. If you need to use it arithmatically, I'd suggest looking at scientific libraries for a class that handles significant figures, because with SF 88.1 != 88.10. The former represents the range 88.05

            J 1 Reply Last reply
            0
            • D Dan Neely

              Judah Himango wrote:

              They're the same in the real mathematical sense of decimal values, as well as the same in the CLR. So, there's no way for you to print out whether it's 88.0 or 88.0000 if you're storing it as a double. I'm not sure whether there's a type in the CLR that stores an arbitrary number of decimal places as part of the value type itself.

              For something not being modifed in code, String. If you need to use it arithmatically, I'd suggest looking at scientific libraries for a class that handles significant figures, because with SF 88.1 != 88.10. The former represents the range 88.05

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              dan neely wrote:

              SF 88.1 != 88.10

              I knew someone would call me out on this. It all depends on the context really; if we're talking about precision, yes, they are obviously not the same. If we're talking about simple arithmetic, they are the same value if precision is ignored (which is often the case in arithmetic). Anyways...for his uses, he'll need to either get a scientific library or write a type that preserves decimal precision, so it seems. I can't find any type in the CLR that preserves decimal precision.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

              C D 2 Replies Last reply
              0
              • C cppdotnet

                This is great but doesn't work if i have 88.0 it will format that to 88.00 I want which shows # as it is ... like 88.0 or 88888.888 Thanks

                Q Offline
                Q Offline
                Qhalis
                wrote on last edited by
                #7

                The problem is that there is no single format that will achieve your goal Say you have 88.0 and 88888.888 Console.WriteLine("{0:0##}", num) will give you 88.0 88888.888 the problem is that if you have another number of finer precision, say 99.1234, it will be rounded to 99.124. If you use the general format g Console.WriteLine("{0:g}", num) then you will get 88 88888.888 99.1234 If you have a hard need to display 88 as 88.0 then you will probably need to use two format strings Dim nums() As Double = {88, 88888.888} Dim numFmt As String For Each num As Double In nums Dim tmpNum As Double = System.Math.Abs(num) If (System.Math.Floor(tmpNum) = tmpNum) Then numFmt = "#.0" Else numFmt = "g" End If Dim exprFmt As String = "{0:" & numFmt & "}" Console.WriteLine(exprFmt) Console.WriteLine(String.Format(exprFmt, num)) Next End Sub NOTE: When testing this, it works well for Doubles but not singles. Don't know why hth, Alan. if (

                1 Reply Last reply
                0
                • J Judah Gabriel Himango

                  dan neely wrote:

                  SF 88.1 != 88.10

                  I knew someone would call me out on this. It all depends on the context really; if we're talking about precision, yes, they are obviously not the same. If we're talking about simple arithmetic, they are the same value if precision is ignored (which is often the case in arithmetic). Anyways...for his uses, he'll need to either get a scientific library or write a type that preserves decimal precision, so it seems. I can't find any type in the CLR that preserves decimal precision.

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

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

                  I know 88 is same as 88.0 or 88.00 but according to my requriments i have two #'s coming in ex 00.00 as string aand 88.00 as decimal .... now i have to compare string # to decimal which means that my number should have 2 digit before decimal and 2 digit after another example one # is 0.0 measn another # should have one digit before decimal and one digit after decimal ... regardless of 8.0 is same as 8.00 or 8 ... in my case only 8.0 or 8.1 0r 8.2 or 9.1 are the valid digit since one digit after decimal and one digit before decimal That's why i can't use formating since i don't what's the 1st # will look like which is string and what's the 2nd # looks like which is decimal Thanks

                  1 Reply Last reply
                  0
                  • J Judah Gabriel Himango

                    dan neely wrote:

                    SF 88.1 != 88.10

                    I knew someone would call me out on this. It all depends on the context really; if we're talking about precision, yes, they are obviously not the same. If we're talking about simple arithmetic, they are the same value if precision is ignored (which is often the case in arithmetic). Anyways...for his uses, he'll need to either get a scientific library or write a type that preserves decimal precision, so it seems. I can't find any type in the CLR that preserves decimal precision.

                    Tech, life, family, faith: Give me a visit. I'm currently blogging about: Connor's Christmas Spectacular! Judah Himango

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #9

                    Judah Himango wrote:

                    dan neely wrote: SF 88.1 != 88.10 I knew someone would call me out on this. It all depends on the context really; if we're talking about precision, yes, they are obviously not the same.

                    true. I mainly included it since I wasn't sure what the OP wanted, and wanted to epxlain why the class was doing what it was and check for potential side effects instead of assuming it was a 'double but keeps empty decimal places' and then getting burned over it enforcing SF, or the like.

                    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