• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

fraction calculation

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can I do this type of calculation in Java like 3/8*5/9=5/24? Or I must define the operation myself using like "BigInteger"? Thanks!

glen
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup u can do such comparisions in java directly

 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shandilya is correct that you can directly to the math 3/8*5/9, however, depending on how you set it up (i.e. what type of primary your answer variable is set up as), you will end up either with a truncated integer (int) of zero, or a floating point number (i.e. a decimal number) of 0.06944444. (And no need to use BigInteger).

If you want your answer to be a fraction, you would need to create a Fraction class. Define it with two int's as instance variables to represent the numerator & denominator. Then create various methods for adding, subtracting, multiplying, etc. You would also probably want a reduce method, and perhaps a findCommonDenominator method. All the things you need to do true fraction math.

Then override Object's toString method to output the result as a fraction.
[ January 15, 2005: Message edited by: Mark Vedder ]
 
glen li
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for making it clear!

glen
 
Attractive, successful people love this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic