rounding decimals

M

man4*.*

example: write a function which will given decimal number round for a
specific number of decimals.
Function has a 2 input parameters(dec. number and number of decimals) and
returns rounded decimal
value. For rounding we can use only int function or similar.
I've pasted my soulution, so please tell me what do you think abut it..give
some advices
my solution:

public class Rounding {
public static double round(double number, int decimalNumber){
int a=(int)Math.pow(10, decimalNumber+1),
b=(int)((number*a)%a)%10;
if (b<5){
double temp=number*a/10;
int temp2=(int)temp;
number=(double)temp2/(a/10);
}
else {
double temp=number*a/10;
int temp2=(int)temp+1;
number=(double)temp2/(a/10);
}
return number;
}
public static void main (String[] args){
double x=7.12345654;
int decimalNumber=4;
System.out.println("Round number:\n"+x+" at "+decimalNumber+" decimals");
System.out.println(round(x,decimalNumber));
}
}
 
C

Chris Brat

Hi,

Suggestions ;
- 'a' , 'b', 'temp' and 'temp2' are not good variable names
- avoid the duplication in your if..else
- use a long or BigDecimal instead of in to prevent overflow
- try not to use logic where you do numerous explicit casts of double
to int (greater chance of overflow)

Regards,
Chris
 
T

Thomas Weidenfeller

man4*.* said:
example: write a function which will given decimal number round for a
specific number of decimals.
Function has a 2 input parameters(dec. number and number of decimals) and
returns rounded decimal
value. For rounding we can use only int function or similar.
I've pasted my soulution,

Thanks for doing so.
so please tell me what do you think abut it..give
some advices

Well, don't you think your teacher should judge your attempt at it and
your accomplishment, instead of the accomplishments of this group's members?

Double-check your solution, then go to your teacher and submit it.
Listen to his comments and remarks.

/Thomas
 
M

man4*.*

Well, don't you think your teacher should judge your attempt at it and
your accomplishment, instead of the accomplishments of this group's
members?

Well, all you say is true, except one thing...
I do not have a teacher...All I have is Thinkgin in Java, few
other books and lot of enthusiasam for learning... ;-)
And for that reason I'm reading every day this newsgroup and
I found it very helpful, and I'm really greatful for all the pople here..

BRG! NHF man.. :)
 
P

Patricia Shanahan

man4*.* said:
example: write a function which will given decimal number round for a
specific number of decimals.
Function has a 2 input parameters(dec. number and number of decimals) and
returns rounded decimal
value. For rounding we can use only int function or similar.

There are some problems with this objective:

1. double is incapable of exactly representing most two decimal place
numbers.

If the numbers that have specific numbers of digits after the decimal
point are particularly important in your application, you should
probably be using BigDecimal, not double.

2. Even if it is just a matter of output formatting, you should use
DecimalFormat rather than doing the rounding yourself. If you need very
specific control over the rounding, convert to BigDecimal and pick from
its collection of rounding modes.

Effectively, you have set yourself a very artificial problem by saying
what features can be used.
public class Rounding {
public static double round(double number, int decimalNumber){
int a=(int)Math.pow(10, decimalNumber+1),
b=(int)((number*a)%a)%10;

Although you can write multiple variable declarations for the same type
in one declaration, it gets a bit muddled unless the declarations are
VERY simple. It also tends to discourage writing long identifiers and/or
comments explaining what the variables mean.

In each case, first pick a really meaningful name for each variable. If
that fully explains what it is about, fine. If not, write a comment
adding whatever information is not in the name.

The conversion to int here will overflow to Integer.MAX_VALUE if
decimalNumber is 9 or greater. If the conversion is just intended to
ensure that the result is an integer, it adds nothing to what is
guaranteed by the API documentation for Math.pow:

"If both arguments are integers, then the result is exactly equal to the
mathematical result of raising the first argument to the power of the
second argument if that result can in fact be represented exactly as a
double value."

I'm not going to write any more until you replace the identifiers "a"
and "b" with meaningful identifiers, with comments if necessary, so that
you tell me what they are for, rather than me trying to guess from how
you use them.

Patricia
 
S

su_dang

man4*.* said:
example: write a function which will given decimal number round for a
specific number of decimals.
Function has a 2 input parameters(dec. number and number of decimals) and
returns rounded decimal
value. For rounding we can use only int function or similar.
I've pasted my soulution, so please tell me what do you think abut it..give
some advices
my solution:

public class Rounding {
public static double round(double number, int decimalNumber){
int a=(int)Math.pow(10, decimalNumber+1),
b=(int)((number*a)%a)%10;
if (b<5){
double temp=number*a/10;
int temp2=(int)temp;
number=(double)temp2/(a/10);
}
else {
double temp=number*a/10;
int temp2=(int)temp+1;
number=(double)temp2/(a/10);
}
return number;
}
public static void main (String[] args){
double x=7.12345654;
int decimalNumber=4;
System.out.println("Round number:\n"+x+" at "+decimalNumber+" decimals");
System.out.println(round(x,decimalNumber));
}
}

Not sure if it is the answer you are looking for, but the
java.math.BigDecimal and java.math.MathContext classes might help
 
M

man4*.*

I'm not going to write any more until you replace the identifiers "a"
and "b" with meaningful identifiers, with comments if necessary, so that
you tell me what they are for, rather than me trying to guess from how
you use them.


First of all thank you a LOT!
This program is not for use anywhere. I know that I could solve
this problem by using BigDecimals, but that's not the point. The
point is to make it on a hard way and try to figure out my own way
of rounding. Such examples are usualy given at interviews for solving
in company where my friend is working. Because I'm learning all by myself
Java, I asked him to give me those examples.
I know that most of you are thinking that I need them to prepare myself for
the interview, but I'm still in a high school and I'm not thinking about
employment right
now... :)
 
P

Patricia Shanahan

man4*.* said:
First of all thank you a LOT!
This program is not for use anywhere. I know that I could solve
this problem by using BigDecimals, but that's not the point. The
point is to make it on a hard way and try to figure out my own way
of rounding. Such examples are usualy given at interviews for solving
in company where my friend is working. Because I'm learning all by myself
Java, I asked him to give me those examples.
I know that most of you are thinking that I need them to prepare myself for
the interview, but I'm still in a high school and I'm not thinking about
employment right
now... :)

You have a few years before you need to deal with weird interview questions.

You would be much better off learning to program well. That includes
finding the simplest way to do things, rather than practicing doing them
in unnecessarily difficult ways.

Patricia
 
C

Chris Uppal

Patricia said:
You would be much better off learning to program well. That includes
finding the simplest way to do things, rather than practicing doing them
in unnecessarily difficult ways.

If by "that" you mean "/learning/ to program well" (my emphasis), then I think
the advice is misleading (although correct). Learning anything involves trying
things out, including things that -- with hindsight -- were mistakes; and
things that are wild and strange not just solid and "normal". Unless you
understand both sides of the boundary between sensible and strange, then you
will not be fitted to judge the difference.

-- chris
 
T

Tris Orendorff

public static void main (String[] args){
double x=7.12345654;
int decimalNumber=4;
System.out.println("Round number:\n"+x+" at "+decimalNumber+" decimals");
System.out.println(round(x,decimalNumber));
}

You should have more than one test in your main program. Try it wth a
negative x, zero and other values. Also try it with decimalNumber > 10,
zero and negative values.

--
Sincerely,

Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top