How to round decimal of double in java

2 weeks ago 11
ARTICLE AD BOX

Do either epsilon comparison or fixed number of iterations.

Epsilon comparison:

ouble epsilon = 1e-6; // adjust for more precision (1e-7) while (high - low > epsilon) { double mid = low + (high - low) / 2.0; if (check(mid)) { low = mid; } else { high = mid; } }

Fixed number of iterations:

for (int i = 0; i < 100; i++) { double mid = low + (high - low) / 2.0; if (check(mid)) { low = mid; } else { high = mid; } }

Deividas Strole's user avatar

2 Comments

Yeah, Thanks I just learned the new concept of using epsilon comparisions.

2026-01-13T18:34:49.02Z+00:00

2026-01-14T02:45:44.88Z+00:00

You can search for your number by assigning a tolerance margin for the result, something like:

double tolerance = 0.000005, objetive = 0.000235, min = objetive - tolerance, max = objetive + tolerance, list[] = { 0.0001, 0.00023, 0.00024 }; for( int i = 0; i < list.length; i++ ) { if( min < list[ i ] && max > list[ i ] ) { System.out.println( "Eureka" ); System.out.println( i ); break; } }

Marce Puente's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article