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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:22:17+00:00 2026-05-18T23:22:17+00:00

I was interested to have the following getNumberOfDecimalPlace function: System.out.println(0 = + Utils.getNumberOfDecimalPlace(0)); //

  • 0

I was interested to have the following getNumberOfDecimalPlace function:

System.out.println("0 = " + Utils.getNumberOfDecimalPlace(0));          // 0
System.out.println("1.0 = " + Utils.getNumberOfDecimalPlace(1.0));      // 0
System.out.println("1.01 = " + Utils.getNumberOfDecimalPlace(1.01));    // 2
System.out.println("1.012 = " + Utils.getNumberOfDecimalPlace(1.012));  // 3
System.out.println("0.01 = " + Utils.getNumberOfDecimalPlace(0.01));    // 2
System.out.println("0.012 = " + Utils.getNumberOfDecimalPlace(0.012));  // 3

May I know how can I implement getNumberOfDecimalPlace, by using BigDecimal?

The following code doesn’t work as expected:

public static int getNumberOfDecimalPlace(double value) {
    final BigDecimal bigDecimal = new BigDecimal("" + value);
    final String s = bigDecimal.toPlainString();
    System.out.println(s);
    final int index = s.indexOf('.');
    if (index < 0) {
        return 0;
    }
    return s.length() - 1 - index;
}

The following get printed :

0.0
0 = 1
1.0
1.0 = 1
1.01
1.01 = 2
1.012
1.012 = 3
0.01
0.01 = 2
0.012
0.012 = 3

However, for case 0, 1.0, it doesn’t work well. I expect, “0” as result. But they turned out to be “0.0” and “1.0”. This will return “1” as result.

  • 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-05-18T23:22:18+00:00Added an answer on May 18, 2026 at 11:22 pm

    This code:

    int getNumberOfDecimalPlaces(BigDecimal bigDecimal) {
        String string = bigDecimal.stripTrailingZeros().toPlainString();
        int index = string.indexOf(".");
        return index < 0 ? 0 : string.length() - index - 1;
    }
    

    … passes these tests:

    assertThat(getNumberOfDecimalPlaces(new BigDecimal("0.001")), equalTo(3));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("0.01")), equalTo(2));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("0.1")), equalTo(1));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("1.000")), equalTo(0));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("1.00")), equalTo(0));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("1.0")), equalTo(0));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("1")), equalTo(0));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("10")), equalTo(0));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("10.1")), equalTo(1));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("10.01")), equalTo(2));
    assertThat(getNumberOfDecimalPlaces(new BigDecimal("10.001")), equalTo(3));
    

    … if that is indeed what you want. The other replies are correct, you have to use BigDecimal all the way through for this rather than double/float.

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

Sidebar

Related Questions

I have the following model setup - a User is interested in projects in
I am interested in the following scenario specifically. Suppose you have team that writes
I know this is largely an opinion, but I'm interested if you have one
I have the following class. Which returns certain fields. I am interested in Reference.
I have the following custom function in ~/.emacs: (defun xi-rgrep (term) (grep-compute-defaults) (interactive sSearch
I have become interested in C-like languages for performance computing. Can you recommend some
I have the following situation: my application's authorization mechanism is implemented using Spring security.
I have the following code. My intention that this custom object can be cast
I have the following string: i:0#.w|domain\x123456 I know about the possibility to group searchterms
I have following jQuery code $('a.editpo, a.resetpass').click(function(event){ event.preventDefault(); var urlToCall = $(this).attr('href'); var hyperlinkid

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.