Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:49:24+00:00 2026-06-04T04:49:24+00:00

The code: import java.math.*; public class x { public static void main(String[] args) {

  • 0

The code:

import java.math.*; 

public class x
{
  public static void main(String[] args)
  {
    BigDecimal a = new BigDecimal(0.1);
    BigDecimal b = new BigDecimal(0.7);
    System.out.println(a);
    System.out.println(b);
  }
}

The output:

0.1000000000000000055511151231257827021181583404541015625
0.6999999999999999555910790149937383830547332763671875

This is nice because it lets me find a double that is the closest to given value.
But as for 0.1 a value is bigger and for 0.7 value is smaller than real value.

How can I get both values (closest bigger and closest smaller) for any decimal number?

Assume I start with BigDecimal, then convert it do a double and then back to decimal. I’ll get bigger or smaller value. How could I got the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:49:26+00:00Added an answer on June 4, 2026 at 4:49 am

    Use nextAfter(double start, double direction):

    import java.math.*;
    
    public class a
    {
      public static void printNeighbours(BigDecimal start)
      {
        double result1 = start.doubleValue();
        double result2;
    
        BigDecimal r1 = new BigDecimal(result1);
    
        int com = start.compareTo(r1);
    
        if(com != 0)
          result2 = Math.nextAfter(result1, com * Double.MAX_VALUE);
        else
        {
          result2 = Math.nextAfter(result1, Double.MAX_VALUE);
          result1 = Math.nextAfter(result1, -Double.MAX_VALUE);
          r1 = new BigDecimal(result1);
        }
    
        BigDecimal r2 = new BigDecimal(result2);
    
        System.out.println("starting:\t"+start);
    
        if(com<0)
        {
          System.out.println("smaller:\t" + r2);        
          System.out.println("bigger:\t\t"+r1);
        }
        else
        {
          System.out.println("smaller:\t" + r1);        
          System.out.println("bigger:\t\t"+r2);
        }
    
        System.out.println();
      }
    
      public static void main(String[] args)
      {
        printNeighbours(new BigDecimal("0.25"));
        printNeighbours(new BigDecimal("0.1"));
        printNeighbours(new BigDecimal("0.7"));
      }
    }
    

    Printout:

    starting:   0.25
    smaller:    0.2499999999999999722444243843710864894092082977294921875
    bigger:     0.250000000000000055511151231257827021181583404541015625
    
    starting:   0.1
    smaller:    0.09999999999999999167332731531132594682276248931884765625
    bigger:     0.1000000000000000055511151231257827021181583404541015625
    
    starting:   0.7
    smaller:    0.6999999999999999555910790149937383830547332763671875
    bigger:     0.70000000000000006661338147750939242541790008544921875
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code sample: import java.math.*; public class x { public static void main(String[] args)
Given the following code : import java.io.*; public class Main { public static void
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Let's look at the following code snippet in Java. package division; import java.math.BigDecimal; final
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
Here is a simple piece of code: import java.io.*; public class Read { public
I'm new to Java. I wrote the following code: import java.io.*; import java.lang.*; public
import java.lang.Math; import java.awt.* public class Triangle implements Shape { java.awt.Point a; java.awt.Point b;
I have the following Java code - import java.lang.reflect.Field; public class AnnotationTest { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.