Results 1 to 8 of 8
Thread: number problem
- 10-10-2010, 10:18 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
number problem
I've a problem ( and i'm a 2 weeks working with java en still learning), the user must give a number p en q, then it must divide p / q ,
if it is a whole number then it may not show the .
for example 10 / 2 this would be 5
but if it is 33/12 then it would me 2.75
No problem with that but extra is that if it is a number like
1/3 = 0,333333333 .. it keeps going, then it must round to 40 numbers after the comma and show … ( three dots, so that they now it is endless. )
I don't know in Java how to make the 1/3 part… I sit stuck at this.
Someone know how to solve this?
Thanks in advantage.
-
Can you use BigDecimal? If so, that's what I'd use here.
- 10-10-2010, 10:24 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
I'm new, but my university sais that everything can be used... so yes but can you explain how it works then?
Thnx for the help btw!
-
BigDecimal will allow you to do basic math operations with an almost arbitrary degree of accuracy. It's helpful for instance if you need to do critical financial calculations, the only downside being that there's a memory and performance cost, but this shouldn't matter to you.
- 10-10-2010, 10:33 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
uhu, i've read a article about it on java website, but still don't see how it is usefull in this case? Because still i can't check if it isn't broken after a couple of numbers...?
- 10-11-2010, 07:30 AM #6
Forget about BigDecimal for the moment (sorry Fubarable).
Given an arbitrary fraction, how would you do this with pen and paper, one digit at a time? How would you stop the process if there aren't any more digits? How would you count 40 digits after the decimal point and put a "..." if the next digit isn't zero?it must round to 40 numbers after the comma and show …
Do that, and then translate your steps to Java code. (hint: multiply, divide and modulo -- * / % -- and of course if(...) or maybe while(...))
db
- 10-11-2010, 09:43 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Let p and q be two positive integers; the fraction p/q either has repeating fractions or not. If not the denominator q has the form 2^n*5^m, otherwise there are repeating fractions. The length of those fractions can easily be found by repeatedly dividing p by q and keep the remaninder. If the remainder has occurred before count the number of modulo operations, that's the length of the repeating fractions.
e.g. p= 5, q= 39; 50%39 == 11, 110%39 == 32, 320%39 == 8, 80%39 == 2, 200%39 == 5; so the length of the repeating fractions equals 5.
kind regards,
Jos
- 10-11-2010, 03:27 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Well if the number is 0,333333335 then i would multiply with 10 then i have 3 , then i would multiply the number with 100 but the i have 33 . And this i would repeat max 40 times, but if the last number is a 0 then I would stop the while lus.. not? But my problem is you get first time 3, second time 33, how can i check that it is zero? ( maybe stupid question but... don't see it yet. )
Similar Threads
-
large number of DB writes: java heap space problem in using transaction-commit
By narimani in forum JDBCReplies: 0Last Post: 06-28-2010, 12:30 PM -
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
[SOLVED] Problem hadling number(money) > 214,74,83,647.99
By playwin2 in forum New To JavaReplies: 2Last Post: 10-14-2008, 09:32 PM -
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11-11-2007, 08:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks