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 double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base

convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base

Scheduled Pinned Locked Moved C#
csharphelptutorial
12 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.
  • S Sakhalean

    Hi All,

    convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base

    Please help

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #3

    While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you. So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in! Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did. If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    S 1 Reply Last reply
    0
    • S Sakhalean

      Hi All,

      convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base

      Please help

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #4

      study these: [^] [^] ... and ask yourself what numeric types it makes sense to convert the values you describe ... to. then study this: [^] and ... start experimenting ! come back with specific questions.

      «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

      S T 2 Replies Last reply
      0
      • B BillWoodruff

        study these: [^] [^] ... and ask yourself what numeric types it makes sense to convert the values you describe ... to. then study this: [^] and ... start experimenting ! come back with specific questions.

        «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

        S Offline
        S Offline
        Sakhalean
        wrote on last edited by
        #5

        Thank you so much. Any have created my own class for converting any base to any base including decimal places (Ex:20.1234). Will share it.

        1 Reply Last reply
        0
        • B BillWoodruff

          study these: [^] [^] ... and ask yourself what numeric types it makes sense to convert the values you describe ... to. then study this: [^] and ... start experimenting ! come back with specific questions.

          «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

          T Offline
          T Offline
          trønderen
          wrote on last edited by
          #6

          BillWoodruff wrote:

          ask yourself what numeric types it makes sense to convert the values you describe

          Why wouldn't it make sense? If b >= 10, you obviously have to define 'digits' for 10+; using A-Z for 10-36 is quite common. Equally obvious: The result of conversion must be treated as a digit string; there is no other way to handle, say, a base 13 number in a binary computer. In decimal, the digits to the left of the decimal point give the number of ones, then number of tens, then ten**2s, ten**3s, ten**4s, ... To the right of the decimal point digits give the number of 1/10s, then the number of 1/(10**2)s, 1/(10**3)s, 1/(10**4)s, ... For base b, the digits to the left of the point give the number of ones, then number of bs, then b**2s, b**3s, b**4s, ... To the right of the point, digits give the number of 1/bs, then the number of 1/(b**2)s, 1/(b**3)s, 1/(b**4)s, ... I guess it would be somewhat confusing to call it a decimal point, though. "Octal is just like decimal ... if you are missing two fingers" (Tom Lehrer) To the OP: I would have split the number on the point, treating the whole and the fractional parts separately, converting then to binary integers, and then iterated each over a divide/remainder down to zero. Note that for the fractional part, you must set a reasonable limit for the number of digits - in, say, base 13 you cannot represent 879/1000 (that is from your first example) as any finite series of fractional digits f1/13 + f2/169 + f3/2197 + ... + fn/(13**n), except for a few special cases. (That goes for decimal .879 or .345 as well; they are not represented by those exact values in double format.)

          B 1 Reply Last reply
          0
          • T trønderen

            BillWoodruff wrote:

            ask yourself what numeric types it makes sense to convert the values you describe

            Why wouldn't it make sense? If b >= 10, you obviously have to define 'digits' for 10+; using A-Z for 10-36 is quite common. Equally obvious: The result of conversion must be treated as a digit string; there is no other way to handle, say, a base 13 number in a binary computer. In decimal, the digits to the left of the decimal point give the number of ones, then number of tens, then ten**2s, ten**3s, ten**4s, ... To the right of the decimal point digits give the number of 1/10s, then the number of 1/(10**2)s, 1/(10**3)s, 1/(10**4)s, ... For base b, the digits to the left of the point give the number of ones, then number of bs, then b**2s, b**3s, b**4s, ... To the right of the point, digits give the number of 1/bs, then the number of 1/(b**2)s, 1/(b**3)s, 1/(b**4)s, ... I guess it would be somewhat confusing to call it a decimal point, though. "Octal is just like decimal ... if you are missing two fingers" (Tom Lehrer) To the OP: I would have split the number on the point, treating the whole and the fractional parts separately, converting then to binary integers, and then iterated each over a divide/remainder down to zero. Note that for the fractional part, you must set a reasonable limit for the number of digits - in, say, base 13 you cannot represent 879/1000 (that is from your first example) as any finite series of fractional digits f1/13 + f2/169 + f3/2197 + ... + fn/(13**n), except for a few special cases. (That goes for decimal .879 or .345 as well; they are not represented by those exact values in double format.)

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #7

            to the flow of such off-topic ramblings, one can only hope the kaopectate of silence will ... help.

            «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

            S 1 Reply Last reply
            0
            • B BillWoodruff

              to the flow of such off-topic ramblings, one can only hope the kaopectate of silence will ... help.

              «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

              S Offline
              S Offline
              Sakhalean
              wrote on last edited by
              #8

              Thank you all. I have created my own class for number conversion from one base to another base including decimal places. Will share it if any body wants it. I am GouseSakhalean, having 17 years of software development experience.

              B T 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you. So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in! Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did. If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                S Offline
                S Offline
                Sakhalean
                wrote on last edited by
                #9

                Sorry, I googled nothing helped me. So I have created my own class for number conversions. I have 17+ years of software development experience. Thanks you so much for your help.

                1 Reply Last reply
                0
                • S Sakhalean

                  Thank you all. I have created my own class for number conversion from one base to another base including decimal places. Will share it if any body wants it. I am GouseSakhalean, having 17 years of software development experience.

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #10

                  Namaskara, "17 years in software development" Then, surely, you are aware that a deep understanding of numeric types in a strongly-typed language, like C#, is essential to competence. In C#, understanding when you can cast, and when you must invoke a conversion method is essential. "rahi gulzar to phool khilenge" Kabir

                  «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

                  1 Reply Last reply
                  0
                  • S Sakhalean

                    Thank you all. I have created my own class for number conversion from one base to another base including decimal places. Will share it if any body wants it. I am GouseSakhalean, having 17 years of software development experience.

                    T Offline
                    T Offline
                    trønderen
                    wrote on last edited by
                    #11

                    Sakhalean wrote:

                    I am GouseSakhalean, having 17 years of software development experience.

                    OK, but then I am surprised that you ask for help with this problem. I consider it to be quite trivial, like an exercise you would be given early in your studies. Furthermore, I can see no practical use for it - except to make a student really look into floating point formats, and to understand the concept of an arbitrary base. In which application do you need to express a double in an arbitrary base? So both the problem and your questioning made it look like some student asking for help to do his homework assignment. I am getting curious to know which real world application (rather than a student exercise) has a need for the conversion function you have described.

                    S 1 Reply Last reply
                    0
                    • T trønderen

                      Sakhalean wrote:

                      I am GouseSakhalean, having 17 years of software development experience.

                      OK, but then I am surprised that you ask for help with this problem. I consider it to be quite trivial, like an exercise you would be given early in your studies. Furthermore, I can see no practical use for it - except to make a student really look into floating point formats, and to understand the concept of an arbitrary base. In which application do you need to express a double in an arbitrary base? So both the problem and your questioning made it look like some student asking for help to do his homework assignment. I am getting curious to know which real world application (rather than a student exercise) has a need for the conversion function you have described.

                      S Offline
                      S Offline
                      Sakhalean
                      wrote on last edited by
                      #12

                      This is for Aerospcase domain project purpose. For Aircraft project where we need for Aircraft parameters to be converted into Deciamal, Octal, hex , binary and engineerig formats.

                      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