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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:04:34+00:00 2026-05-19T06:04:34+00:00

I have a variable of type double , I need to print it in

  • 0

I have a variable of type double, I need to print it in upto 3 decimals of precision but it shouldn’t have any trailing zeros…

eg. I need

2.5 // not 2.500
2   // not 2.000
1.375 // exactly till 3 decimals
2.12  // not 2.120

I tried using DecimalFormatter, Am i doing it wrong?

DecimalFormat myFormatter = new DecimalFormat("0.000");
myFormatter.setDecimalSeparatorAlwaysShown(false);

Thanks. 🙂

  • 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-19T06:04:35+00:00Added an answer on May 19, 2026 at 6:04 am

    Try the pattern "0.###" instead of "0.000":

    import java.text.DecimalFormat;
    
    public class Main {
        public static void main(String[] args) {
            DecimalFormat df = new DecimalFormat("0.###");
            double[] tests = {2.50, 2.0, 1.3751212, 2.1200};
            for(double d : tests) {
                System.out.println(df.format(d));
            }
        }
    }
    

    output:

    2.5
    2
    1.375
    2.12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help, For example I have a decimal type variable and I
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have a variable of type Dynamic and I know for sure one of
I have a variable of type Number, and i like to obtain the sign
I have a variable of type Hashmap <String,Integer >. In this, the Integer value
Suppose I have a variable of type Int = 08, how can I convert
I have a reference-type variable that is readonly , because the reference never change,
I want to add the thousands separator to a variable of type double .
In C# using VS2005, if I have a variable of type Object , to
I have a variable FirstThreads of type List<Thread> . I am trying to do

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.