• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Decimals not converting to correct fractions

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Awhile back, I found a class that takes a decimal value and converts it to a fraction. It seemed to have been working OK, but a couple of users noticed some decimal values not converting properly.
The two they've noticed so far are :
.781 should convert to 25/32, but it's converting to 7/9.
.625 should convert to 3/8, but it's converting to 3/5.
I tried to look at the code and figure out what was going on, but I guess I really don't understand everything that is going on to get fraction values.
Could someone take a look at the method that is supposed to be converting the value to a fraction, and help me understand why it is doing it?
Here's the method: (Sorry it's so long)

I appreciate ANY help anyone can give me on this. Thanks again!!
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

[ November 14, 2003: Message edited by: Tom Blough ]
[ November 14, 2003: Message edited by: Tom Blough ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using continued fractions (I hope I've done this correctly since it was very late ):
 
expectation is the root of all heartache - shakespeare. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic