Round
Hi all,
I need to implement a method that return a double value, but this value should be rounded first. For example, if the final result was 10.8356 it should returns 10.84. I tried to play with the characters and use if statement, but this seems unpractical.
Any help?
Mora2Ali
[304 byte] By [
Mora2Alia] at [2007-11-26 22:41:19]

Have a look at DecimalFormat: http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
Thanks alot. Here is a new thing that I learned, but can you specify for me which API will help me in rounding the double number?
[url= http://java.sun.com/j2se/1.5.0/docs/api/index.html]RTFM[/url]
This dosne't help. You gave exactly what hphein gave me before you. Can someone tell me a simple algorithm to wite a method to round any double number, and I can specify at the same time how many digits I wana round!
I always use the static Math class methods provided as part of the core classes.
Math.round(double d) returns a long.
Math.round(float f) returns an int.
It rounds your decimal number to a whole integer so you have to do a little manual manipulation with your number if you want to round it to a specific decimal point.
Multiply the double by a factor of 10 for each decimal point you want to round to, then it will round to the result. Then just divide the result by the same factor of 10 to retrieve the correctly rounded number.
Eg. 10.84789 rounded to the nearest 100th will be figured like this.
double decimalNumber = 10.84789;
double roundedDecimal = ((double)Math.round(decimalNumber * 100.0)) / 100.0;
> This dosne't help. You gave exactly what hphein gave
> me before you.
>
> Can someone tell me a simple algorithm to wite a
> method to round any double number, and I can specify
> at the same time how many digits I wana round!
Read this tutorial too:
http://java.sun.com/docs/books/tutorial/java/data/index.html
Thanks alot maple. You really helped me. Now I'll continue the rest of the coding :)
> This dosne't help. You gave exactly what hphein gave
> me before you.
>
> Can someone tell me a simple algorithm to wite a
> method to round any double number, and I can specify
> at the same time how many digits I wana round!
the algorithm could be:
...
// searching on google
List<Result> results=searchOnGoogle("round+double+java");
if(results!=null){
for(Result r:results){
if(readAndSearchInTheResultForAGoodAndUnderstandableSolution(r)){
if(solutionIsWorkingForMyProblem(r)){
System.out.println("solution: "+r.getSolution().toString());
break;
}
}
}
}else{
// java forums...
goToAskItOnJavaForumsBecauseICantFindNothingOnTheWeb("round+double");
}
Thanks alot maple. You really helped me. Now I'll continue the rest of the coding :) for you supareno if you can't read!!
> Thanks alot maple. You really helped me. Now I'll
> continue the rest of the coding :)
>
> for you supareno if you can't read!!
// code based reply#5
double firstDouble=10.132456;
double roundedFirstDouble = ((double)Math.round(firstDouble * 100.0)) / 100.0;
System.out.println("firstDouble: "+roundedFirstDouble);
// code based on reply#1
double secondDouble=10.132456;
DecimalFormat df=new DecimalFormat("00.00");
System.out.println("secondDouble: "+df.format(secondDouble));
and i cannot read?
Dukes for me and Supareno?5 for me 5 for him cuz he gave just as good a solution?
not for me, i don't want the dukes...
> not for me, i don't want the dukes...Just trying to be fair.Something tells me he is not ever going to feed the dukes. :(