Solved

java rounding problem

Posted on 2002-07-30
10
296 Views
Last Modified: 2012-05-04
i want to ask someone,
how to round the decimal like this:
from 10.48 to 10.45
from 289.41 to 289.40 ???
ASAP
0
Comment
Question by:kankan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:nir2002
ID: 7188036
Hi,

you mean 10.48 => 10.5
If need to round to the first digit after the floating point.

x (double poistive value or positive float)
x = Math.floor(x*10+0.5)/10.0;

for negtive use

x = Math.ceil(x*10-0.5)/10.0;

Hope it help
Best regards
Nir
0
 
LVL 35

Expert Comment

by:girionis
ID: 7188172
 This is a really weird rounding you are asking. Did you actualyl mean rounf the 10.48 to 10.50 instead? If yes below is a quick fix:

static BigDecimal format(BigDecimal n, int prec, int round)
{
    return n.setScale(prec, round);
}

public static void main(String[] args)
{
    BigDecimal bd = format(new BigDecimal("10.48"), 1, BigDecimal.ROUND_HALF_UP);
    System.out.println(format(bd, 2, BigDecimal.ROUND_UNNECESSARY));
     bd = format(new BigDecimal("289.41"), 1, BigDecimal.ROUND_HALF_DOWN);
         System.out.println(format(bd, 2, BigDecimal.ROUND_UNNECESSARY));
}

  If you indeed meant round the 10.48 to 10.45 I will have to put more thought in it. There is also a good tutorial on BigDecimal. It might come in handy:

http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html

  Hope it helps.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7188178
 The: new BigDecimal("10.48") can also be: new BigDecimal(10.48) as well.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:ksivananth
ID: 7189733
Hope this helps,

    private final double roundValue(double val)
    {
           java.math.BigDecimal bdVal = new java.math.BigDecimal(""+val);

          bdVal = bdVal.setScale(2, java.math.BigDecimal.ROUND_HALF_UP);

          return bdVal.doubleValue();
    }
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7189818
Hi,
 
public static double roundValue(double val,int floating_point_digit_to_round )  {
   double round_result;
   double factor = Math.pow(10.0,(double)floating_point_digit_to_round );
   if(val>0) {
      round_result = Math.floor(val*factor)+0.5)/factor;
   } else {
      round_result = Math.ceil(val*factor)+0.5)/factor;
   }
   return round_result;
}

general function to round floating point
for your case use floating_point_digit_to_round = 1
This solution is more effiective (performance) than the other since you don't need to create temporary objects.

Best regards
Nir
0
 

Author Comment

by:kankan
ID: 7190274
actually, no,
i just want
e.g. 14.48 it will be 14.45
round down to 5
or if 14.41 it will be 14.40
not rounding``
0
 

Author Comment

by:kankan
ID: 7190279
actually, no,
i just want
e.g. 14.48 it will be 14.45
round down to 5
or if 14.41 it will be 14.40
not rounding``
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7190310
you want >0.45 => 0.45 and <0.45 =>0.40 ?
0
 

Author Comment

by:kankan
ID: 7192593
actually,

if 0.45 => 0.45
if 0.41 => 0.40
if 0.48 => 0.45
if 1.59 => 1.55
if 1.53 => 1.50
can u get it ??
ASAP
Thanks
0
 
LVL 2

Accepted Solution

by:
nir2002 earned 100 total points
ID: 7192738
Hi

it is uncommon rounding what you need.

public static double speicalRoundDown(double value,int digit) {
     int newDigit;
     int difference,absDifference;
     int digitToRound;
     int manipulateValue;
     double returnValue;
     manipulateValue = (int)(value*Math.pow(10.0,(double)digit));
     digitToRound = manipulateValue % 10;
     difference = digitToRound - 5;
     absDifference = Math.abs(difference);
     newDigit = (difference + absDifference) / 2 - digitToRound*(difference - absDifference)/2/Math.max(absDifference,1);
     manipulateValue = manipulateValue - newDigit;
     returnValue = ((double)manipulateValue)/Math.pow(10.0,(double)digit);
     return returnValue;          
}

that should do the trick.
for your case:
speicalRoundDown(10.47,2);
speicalRoundDown(289.41,2);

Best regards
Nir
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which non-HTML GUI front end to use with Java? 3 67
dao vs facade design patterns 2 76
Java ArrayList and if statement 2 55
Is there a Google Voice API? 9 75
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question