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]
# 1
Have a look at DecimalFormat: http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
PhHeina at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 2
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?
Mora2Alia at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 3
[url= http://java.sun.com/j2se/1.5.0/docs/api/index.html]RTFM[/url]
suparenoa at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 4
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!
Mora2Alia at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 5

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;

maple_shafta at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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

PhHeina at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks alot maple. You really helped me. Now I'll continue the rest of the coding :)
Mora2Alia at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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");

}

suparenoa at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 9
Thanks alot maple. You really helped me. Now I'll continue the rest of the coding :) for you supareno if you can't read!!
Mora2Alia at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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?

suparenoa at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 11
Dukes for me and Supareno?5 for me 5 for him cuz he gave just as good a solution?
maple_shafta at 2007-7-10 11:56:09 > top of Java-index,Java Essentials,Java Programming...
# 12
not for me, i don't want the dukes...
suparenoa at 2007-7-10 11:56:10 > top of Java-index,Java Essentials,Java Programming...
# 13
> 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. :(
maple_shafta at 2007-7-10 11:56:10 > top of Java-index,Java Essentials,Java Programming...