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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:59:52+00:00 2026-06-01T04:59:52+00:00

I have double values which I would like to convert to String values with

  • 0

I have double values which I would like to convert to String values with the following format restrictions:

number_of_fraction_digits = max( 0, 5 – number_of_integer_digits )

Essentially I want to keep the number of digits to 5 if possible, rounding decimal digits if necessary. For example:

 float          String
-------------------------
     1               1
   100             100
100000          100000
 99999           99999
 99999.99        99999
  9999.99         9999.9
   999.99          999.99
    23.34324        23.343

I’ve looked into using DecimalFormat but as far as I can tell it doesn’t quite accomplish what I’m after.

It allows setting of the maximum number of decimal digits using setMaximumFractionDigits() but as far as I can tell I would have to calculate the number of integer digits and perform the above calculation myself.

So the basic question is whether there is a nice, clean built-in way to format numbers in this way.

  • 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-06-01T04:59:54+00:00Added an answer on June 1, 2026 at 4:59 am
    public class SignificantFormat {
    
        public static String formatSignificant(double value, int significant)
        {
            MathContext mathContext = new MathContext(significant, RoundingMode.DOWN);
            BigDecimal bigDecimal = new BigDecimal(value, mathContext);
            return bigDecimal.toPlainString();
        }
    
        public static void main(String[] args) {
            double[] data = { 1, 100, 100000, 99999, 99999.99, 9999.99, 999.99, 23.34324 };
            for(double d: data){
                System.out.printf("Input: %10s \tOutput: %10s\n", Double.toString(d), formatSignificant(d, 5));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some double values I want to convert to a string with this
Basicly what I would like to is generate Other random values which have not
I have table which contains double values stored in mysql database ...I need to
I have a string that contains a known number of double values. What's the
I have several TextBoxes, which display angular values. I format these using different custom
I have a Canvas, to which I've added several thousand polygons. I would like
I have a DecimalFormat object which I'm using to format all of my double
I have the following problem: I have a function which takes a List[Double] as
I have a dropdownlist which I would like to be optional, however, the ModelState.IsValid
I have a two-dimension ArrayList that contains double values: ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>();

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.