Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

rounding decimals

Thread view: 
man4*.* - 06 Oct 2006 12:33 GMT
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));
}
}
Chris Brat - 06 Oct 2006 13:05 GMT
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
Thomas Weidenfeller - 06 Oct 2006 13:11 GMT
> 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
Signature

The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

man4*.* - 06 Oct 2006 14:03 GMT
> 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.. :-)
Patricia Shanahan - 06 Oct 2006 15:49 GMT
> 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
man4*.* - 06 Oct 2006 20:32 GMT
> 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... :-)
Patricia Shanahan - 06 Oct 2006 20:52 GMT
>> 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
[quoted text clipped - 12 lines]
> 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
Chris Uppal - 07 Oct 2006 12:13 GMT
> 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
su_dang@hotmail.com - 06 Oct 2006 15:52 GMT
> example: write a function which will given decimal number round for a
> specific number of decimals.
[quoted text clipped - 28 lines]
>  }
> }

Not sure if it is the answer you are looking for, but the
java.math.BigDecimal and java.math.MathContext classes might help
Tris Orendorff - 09 Oct 2006 16:48 GMT
>  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.

Signature

Sincerely,

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.