Results 1 to 2 of 2
  1. #1
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default decimal to simplest form fraction

    I'm making a simple calculator with the ability to convert a decimal figure, like 1.25 in to 1 1/4.

    However, programming and math are both flinching areas for me.

    Say my calculator screen says 1.28.

    I've been thinking I could use the "." as a break point and grab the real numbers that way, and count the decimals after it. That way I can make it in to a fraction, but then I can't figure out how to bring it down to simplest terms.

    In theory, I could use indexOf on the "." and somehow grab the real number from before it and count the decimals after it.

    Now I would have 1 28/100. Simple enough I could just divide by two, but as the decimal figure can be anything, dividing by two won't always obviously cut it.

    Help a noob, would ya? :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: decimal to simplest form fraction

    If you can find a numerator N and a denominator D, divide both of them by gcd(N, D).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 10:59 AM
  2. Simplest code won't work, help needed
    By goose87 in forum New To Java
    Replies: 5
    Last Post: 10-30-2010, 06:17 PM
  3. Decimal to Fraction method
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 07-16-2009, 02:42 AM
  4. Simplest SWT application
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:12 PM
  5. Simplest ApplicationWindow Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •