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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:53:44+00:00 2026-05-30T14:53:44+00:00

How to number formating with Java in various scenarios: if the value is 0,then

  • 0

How to number formating with Java in various scenarios:

  1. if the value is 0,then output will be zero.
  2. if the value 1,then output will be 1.
  3. if the value is 1.2,then output will be 1.20.
  4. if the value is 1.20,then output will be 1.20.

So it means if the input value has decimals then i have to apply numberformat to two decimal places otherwise not required.

  • 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-30T14:53:45+00:00Added an answer on May 30, 2026 at 2:53 pm

    One DecimalFormatter isn’t going to work for the case of no decimal and the case of two decimal places.

    Here’s some code that meets all 4 of your conditions:

        DecimalFormat formatter1 = new DecimalFormat("0");
        DecimalFormat formatter2 = new DecimalFormat("0.00");
    
        double[] input = {0, 1, 1.2, 1.265};
        for (int i = 0; i < input.length; i++) {
            double test = Math.round(input[i]);
            if (Math.abs(test - input[i]) < 1E-6) {
                System.out.println(formatter1.format(input[i]));
            } else {
                System.out.println(formatter2.format(input[i]));
            }
        }
    

    Edited to add: For jambjo, a version that manipulates the String after the DecimalFormatter.

        DecimalFormat formatter2 = new DecimalFormat("0.00");
    
        double[] input = {0, 1, 1.2, 1.265};
        for (int i = 0; i < input.length; i++) {
            String result = formatter2.format(input[i]);
            int pos = result.indexOf(".00");
            if (pos >= 0) {
                result = result.substring(0, pos);
            }
            System.out.println(result);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Eclipse has a huge number of formatting rules for Java and Javascripting, which can
I have a 2 dimensional array forming a table: [color][number][shape ] ------------------------- [black][10 ][square
I have an application that runs on both C# .Net and Java . Two
In Java I have a long integer representing a period of time in milliseconds.
I have a (java) program that prints a line of hex numbers to stdout
I have an html page with JavaScript code for setting the phone number format
This question asking for a phone number format API in Java got me wondering
Does Java and/or Spring have the concept of properties? I have bunch of domain
I have a Windows Form with data bound textBox which displays a phone number
Does any one have sample code to apply phone number formatting to certain fields???

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.