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 72703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:05:10+00:00 2026-05-10T20:05:10+00:00

Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return

  • 0

Currently i have this method:

static boolean checkDecimalPlaces(double d, int decimalPlaces){     if (d==0) return true;      double multiplier = Math.pow(10, decimalPlaces);      double check  =  d * multiplier;     check = Math.round(check);           check = check/multiplier;      return (d==check);       } 

But this method fails for checkDecmialPlaces(649632196443.4279, 4) probably because I do base 10 math on a base 2 number.

So how can this check be done correctly?

I thought of getting a string representation of the double value and then check that with a regexp – but that felt weird.

EDIT: Thanks for all the answers. There are cases where I really get a double and for those cases I implemented the following:

private static boolean checkDecimalPlaces(double d, int decimalPlaces) {     if (d == 0) return true;      final double epsilon = Math.pow(10.0, ((decimalPlaces + 1) * -1));      double multiplier = Math.pow(10, decimalPlaces);     double check = d * multiplier;     long checkLong = (long) Math.abs(check);     check = checkLong / multiplier;      double e = Math.abs(d - check);     return e < epsilon; } 

I changed the round to a truncation. Seems that the computation done in round increases the inaccuracy too much. At least in the failing testcase.
As some of you pointed out if I could get to the ‘real’ string input I should use BigDecimal to check and so I have done:

BigDecimal decimal = new BigDecimal(value); BigDecimal checkDecimal = decimal.movePointRight(decimalPlaces); return checkDecimal.scale() == 0; 

The double value I get comes from the Apache POI API that reads excel files. I did a few tests and found out that although the API returns double values for numeric cells I can get a accurate representation when I immediately format that double with the DecimalFormat:

DecimalFormat decimalFormat = new DecimalFormat(); decimalFormat.setMaximumIntegerDigits(Integer.MAX_VALUE); // don't use grouping for numeric-type cells decimalFormat.setGroupingUsed(false); decimalFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); value = decimalFormat.format(numericValue); 

This also works for values that can’t be represented exactly in binary format.

  • 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. 2026-05-10T20:05:11+00:00Added an answer on May 10, 2026 at 8:05 pm

    The test fails, because you have reached the accuracy of the binary floating point representation, which is approximately 16 digits with IEEE754 double precision. Multiplying by 649632196443.4279 by 10000 will truncate the binary representation, leading to errors when rounding and dividing afterwards, thereby invalidating the result of your function completely.

    For more details see http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

    A better way would be to check whether the n+1 decimal places are below a certain threshold. If d - round(d) is less than epsilon (see limit), the decimal representation of d has no significant decimal places. Similarly if (d - round(d)) * 10^n is less than epsilon, d can have at most n significant places.

    Use Jon Skeet‘s DoubleConverter to check for the cases where d isn’t accurate enough to hold the decimal places you are looking for.

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

Sidebar

Ask A Question

Stats

  • Questions 115k
  • Answers 115k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer safeWrap returns a function that sets a timeout of 0ms… May 11, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer Assuming you have the html text in the string "foo",… May 11, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer Your third answer is the best method. A sprint is… May 11, 2026 at 10:23 pm

Related Questions

I've recently started using code coverage tools (particularily Emma and EclEmma), and I really
I'm currently working on a class that calculates the difference between two objects. I'm
Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that
I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.