File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "function" Watch "function" New topic


Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265

Can any body give the idea for the following function ...

Where x = zeros to be rounded off to
a.Divide the variable by 10^x
b.Round up the value so arrived at
c.Multiply the value by 10^x again.

If round(21,1)
The function should perform the following activities:
Step a Output: 2.1
Step b Output: 2
Step c Output (Final output) = 2 * 10 = 20

If round(27,1)
The function should perform the following activities:
Stepa Output: 2.7
Step b Output: 3
Step c Output (Final output) = 3 * 10 = 30

If round(19876,3)
The function should perform the following activities:
Stepa Output: 198.76
Step b Output: 20
Step c Output (Final output) = 20 * 1000 = 20000

Can any body give me the good way of handling the above function ?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Uhm...its hard to go into more detail than your assignment already gives you. People here don't tend to write code for you, rather suggest ways you can write it yourself. But since the question gives you pretty much all the detail you would probaly need, I can't see what else to add. What specifically is confusing you?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

start small.

write a function that lets you pass in two variables, and print them out.

once that works, the next thing you need to to is compute 10^x. write that code and print out your result to make sure IT works.

at each step, TEST TEST TEST. i might even suggest leaving in all the print statements until you are done. the ones your just using for debug, you can do something like

System.out.println("DEBUG: blah blah blah");

then, when your all finished and are SURE everything works, you can quickly find the print statements with the word DEBUG and remove them.

Note that there are better ways of doing this, but this is pretty simple, and will get you pretty far...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1046
Also look at java.lang.Math.pow() and round().
I agree. Here's the link:
subject: function
It's not a secret anymore!