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 / C++ / MFC
  4. decimal fraction conversion

decimal fraction conversion

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
3 Posts 3 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.
  • A Offline
    A Offline
    Aaron Knox
    wrote on last edited by
    #1

    Hello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer

    A J 2 Replies Last reply
    0
    • A Aaron Knox

      Hello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      I know how you feel. Before I took a programming class, I didn't have a clue how to translate a mathematical algorithm into code. Taking that C class in college really helped. Perhaps you should try that yourself. If you have taken a class and/or read a few books, then you should at least attempt to convert your algorithm into code. Once you do, compile your code and see if it works. If you get stuck, come back here and show us your work so we can help you pinpoint the problem. Good luck! Regards, Alvaro


      That which does not kill me postpones the inevitable. -- despair.com

      1 Reply Last reply
      0
      • A Aaron Knox

        Hello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer

        J Offline
        J Offline
        Josh Knox
        wrote on last edited by
        #3

        Hey Dude, A quick 'n' dirty way of doing it would be to count the number of digits after the decimal point as your numerator and use this number of digits to calculate your denominator. Ex: .3456 = 3456/10000 That'd give you a fraction at least. If you want to reduce it, find numbers that divide both the numerator and denominator evenly. Use the modulus operator for this. A result of 0 indicates that the number is evenly divisible. ex: 2%10=0, evenly divisible 3%10=1, not evenly divisible. Of course for very long decimals you may want to adopt some fixed amount of places to round off to. You will lose some precision in the number however. I'd put the whole operation in a function like: void print_fraction(double decimal_value), that prints a fraction for the decimal value you pass to it. Hope this helps. Josh Knox

        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