0.43 convert to 43/100??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hhkangyong
    New Member
    • Aug 2006
    • 2

    0.43 convert to 43/100??

    umm.. stretching my head..
    how to make the code to convert 0.43 to 43/100?
    any expert can help me ??
    thx alot..
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Sorry that question doesn't really make sense because you have left out data types.

    0.43 = 43/100

    there is no need for a conversion.

    Comment

    • rgb
      New Member
      • Aug 2006
      • 37

      #3
      if you assume that input is a string, you can use a combination of:

      strchr( ) function - http://www.cplusplus.com/ref/cstring/strchr.html
      -- to locate the "."

      strncpy( ) function - http://www.cplusplus.com/ref/cstring/strchr.html
      -- to get string after "."

      strlen( ) function - http://www.cplusplus.com/ref/cstring/strlen.html
      -- to get how many "0" you want to use on denominator (multiple of 10s)

      strcat( ) function - http://www.cplusplus.com/ref/cstring/strcat.html
      -- to concatinate your final string (answer)

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        I'm not sure converting a decimal into a rational number can be done accurately. First, computers only store decimals to so much accuracy. Therefore, we can't necessarily know whether a number repeats or not. The computer stores in binary.

        decimal 0.43 = binary 0.0110111000001 ...

        In fact, in order for the decimal number to be a finite binary number, it must end with a five. Even then, it may not be stored accurately if the number is 0.1987651321949 865132168795, for example.

        Otherwise, suppose the number input is binary 0.1101011001010 1
        Then, convert this to 11010110010101/100000000000000 . Then calculate gcd(11010110010 101,10000000000 0000). Divide both 11010110010101 and 100000000000000 by the result, and that's the best you can do.

        0.1101011001010 1 = (11010110010101/gcd(11010110010 101,10000000000 0000)/(10000000000000 0/gcd(11010110010 101,10000000000 0000))

        Comment

        Working...