Unconfigured Ad Widget

Collapse

Announcement

Collapse
No announcement yet.

Converting decimals to fractions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting decimals to fractions

    Ckasher wrote: > I might be getting a project where I need to convert decimals to a > fraction equivalent. Example 5.50 to 5 1/2, 4.750 to 4 3/4. Was > looking for any input on this. Thanks Craig Depends upon how many possible combinations you might have and which language you have to use. If it were in RPG and there were a "reasonable" number of combinations, I'd use an alternating table. Search the table with the decimal portion of the number and it will return the appropriate fraction. Same concept would be 2 arrays, search the first and use the element index to grab the value from the second. Bill

  • #2
    Converting decimals to fractions

    I might be getting a project where I need to convert decimals to a fraction equivalent. Example 5.50 to 5 1/2, 4.750 to 4 3/4. Was looking for any input on this. Thanks Craig

    Comment


    • #3
      Converting decimals to fractions

      Here's some code that might get you going. I can't say that this code is the best approach, efficient, or even correct! But it did produce the correct results for the random examples shown. The idea is to call procedure FRACTION with the digits to the right of the decimal point, and be returned a string containing the fraction representation. I'll 'fess up to the method used to reduce the fraction: http://en.wikipedia.org/wiki/Euclidean_algorithm Tom D.
      Code

      Comment

      Working...
      X