# function

Graham Thorpe
Ranch Hand
Posts: 265
Hi

Can any body give the idea for the following function ...
round(<variablename>,x)

Where x = zeros to be rounded off to
Notes
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
Bartender
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?

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
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...

Ryan McGuire
Ranch Hand
Posts: 1085
4
Also look at java.lang.Math.pow() and round().