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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:21:03+00:00 2026-05-27T21:21:03+00:00

I know how to format a double to keep only the available decimal places

  • 0

I know how to format a double to keep only the available decimal places (DP), up to a certain number of DPs. This example keeps up to 4 DPs.

    double d = 1.0;
    DecimalFormat df = new DecimalFormat("#.####");
    System.out.print(df.format(d)); // returns "1"

    double d = 1.23;
    DecimalFormat df = new DecimalFormat("#.####");
    System.out.print(df.format(d)); // returns "1.23"

    double d = 1.2345678;
    DecimalFormat df = new DecimalFormat("#.####");
    System.out.print(df.format(d)); // returns "1.2346", rounding off: bad!

Now I want whole numbers e.g. 1.0 to return "1" without the unnecessary .0, and the # format character does provide that functionality. But how do I make sure that the number never gets rounded off? Is there any other way other than an arbitrarily long chain of # such as "#.###########################################"?

Or should I just use the default conversion of double to string, and truncate the ".0" if it appears at the end:

    String s = "" + d;
    if(s.substring(s.length()-2).equals(".0")) {
        s=s.substring(0, s.length()-2);
    }

Both ways seems terribly clumsy.

  • 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-27T21:21:04+00:00Added an answer on May 27, 2026 at 9:21 pm

    I use the following

    double d =
    String s = (long) d == d ? "" + (long) d : "" + d; 
    

    if you need Double instead for double. (Personally I would avoid using the wrapper if you can)

    Double d =
    String s = d.longValue() == d ? "" + d.longValue() : "" + d; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know the format of the MAPI property PR_SEARCH_KEY ? The online documentation
Does anybody know which date format is to be used, trying to create a
How can I know what date format user prefers? Is there a way to
I want to know in which format the contact details are stored in android
I know of the US GIS TIGER file format from years ago, but have
I know that Matlab allows for you to format the graph after its created
Does anyone know how I can get a format string to use bankers rounding
Does anyone know how to construct a format string in .NET so that the
Does anyone know how to display a percentage format based on the locale using
I don't know how to show Time in AM/PM format in blackberry using blackberry

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.