File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes problem in round off Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in round off" Watch "problem in round off" New topic
Author

problem in round off

bala manian
Ranch Hand

Joined: Nov 05, 2007
Posts: 40
i want to round off my values to 2 decimal points.
example:
my value 7.826, needed roundoff values 7.85 not as 7.83.i need java coding for these.thanks in advances
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

how are you getting 7.85 from 7.826? what exactly are you requirements here? we can't tell if you're always rounding up, if you're rounding to the closes 1/20th (i.e. either .x5 or .x0) if you're rounding up at least .02 or something else.

Please provide more details on what you want.


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

Joined: Nov 05, 2007
Posts: 40
my clients expecting round off value as 7.85, if he gets 7.826.what should i do for these?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

If that is really what your client wants, then your client wants some strange, non-standard way of rounding numbers. As Fred already said, you have to find out first what rules for rounding your client wants exactly. We cannot guess here what your client wants. So, go and talk to them to find out exactly what they want.

Rounding 7.826 to 7.85 is not logical.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Brandon DuRette
Smart Bear Support
Greenhorn

Joined: Jul 06, 2009
Posts: 8
You need to generalize what your client wants. As Fred tried to explain, one specific example -- especially the one you gave -- is not enough to derive an appropriate rounding algorithm.

A further consideration is what is the type of the number that you are rounding and what is the purpose of the data? You may be needing an arbitrary precision decimal (java.math.BigDecimal) which you can round using many different rounding modes (java.math.RoundingMode).


Code Review Tools - Code Review and More - The Smart Bear Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in round off