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

  • SEARCH
  • Home
  • 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 8457525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:49:49+00:00 2026-06-10T12:49:49+00:00

I am building a web app in Java that does math and shows steps

  • 0

I am building a web app in Java that does math and shows steps to the user. When doing basic arithmetic with decimals I often get the messy in accurate outputs.

Here is my problem:

double a = 0.15;
double b = 0.01;
System.out.println(a - b);
// outputs 0.13999999999999999

float a = 0.15;
float b = 0.01;
System.out.println(a - b);
// outputs 0.14

float a = 0.16f;
float b = 0.01f;
System.out.println(a - b);
// outputs 0.14999999

double a = 0.16;
double b = 0.01;
System.out.println(a - b);
// outputs 0.15

Neither is reliable for complete accuracy. Is there a numeric class that is more precise or should I just round the values off?

  • 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-10T12:49:51+00:00Added an answer on June 10, 2026 at 12:49 pm

    You can use BigDecimal for this. It’s ugly, but it works:

    BigDecimal a = new BigDecimal("0.15");
    BigDecimal b = new BigDecimal("0.01");
    System.out.println(a.subtract(b));
    

    Be sure to construct them either with a String parameter, or with the valueOf method, like this:

    BigDecimal x = new BigDecimal("0.15");   // This is ok
    BigDecimal x = BigDecimal.valueOf(0.15); // This is also ok
    

    And not with a double parameter, like this:

    BigDecimal x = new BigDecimal(0.15); // DON'T DO THIS
    

    Because if you pass in a double, you will also pass in double’s inaccuracy into the new BigDecimal instance. If you pass in a String, BigDecimal will know™ and do the right thing™.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Java web app that needs access to a user's Google Calendar
I'm building a Google App Engine web app, with a Java back end, that
I'm currently working on building a java web app. I've been looking to use
I'm building a web app that's going to support multiple languages. For the moment,
I'm building a web app that integrates with Google Drive, and am wondering if
I'm building a web app that relies quite heavily on email notification/responses. I obviously
I am building a GWT app that uses Web SQL Local Storage ( http://dev.w3.org/html5/webdatabase/
I'm am building a web app with app engine (java) and GWT, although I
I am building a web app that has a class called Product, which I'd
I learned building a web-app with Java and Seam (and JSF 1.2) But now

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.