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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:27:01+00:00 2026-05-26T18:27:01+00:00

I am looking for a way to format a floating point number dynamically in

  • 0

I am looking for a way to format a floating point number dynamically in either standard decimal format or scientific notation, depending on the value of the number.

  • For moderate magnitudes, the number should be formatted as a decimal with trailing zeros suppressed. If the floating point number is equal to an integral value, the decimal point should also be suppressed.
  • For extreme magnitudes (very small or very large), the number should be expressed in scientific notation. Alternately stated, if the number of characters in the expression as standard decimal notation exceeds a certain threshold, switch to scientific notation.
  • I should have control over the maximum number of digits of precision, but I don’t want trailing zeros appended to express the minimum precision; all trailing zeros should be suppressed.

Basically, it should optimize for compactness and readability.

2.80000 -> 2.8

765.000000 -> 765

0.0073943162953 -> 0.00739432 (limit digits of precision—to 6 in this case)

0.0000073943162953 -> 7.39432E-6 (switch to scientific notation if the magnitude is small enough—less than 1E-5 in this case)

7394316295300000 -> 7.39432E+6 (switch to scientific notation if the magnitude is large enough—for example, when greater than 1E+10)

0.0000073900000000 -> 7.39E-6 (strip trailing zeros from significand in scientific notation)

0.000007299998344 -> 7.3E-6 (rounding from the 6-digit precision limit causes this number to have trailing zeros which are stripped)


Here’s what I’ve found so far:

  • The .toString() method of the Number class does most of what I want, except it doesn’t upconvert to integer representation when possible, and it will not express large integral magnitudes in scientific notation. Also, I’m not sure how to adjust the precision.
  • The "%G" format string to the String.format(...) function allows me to express numbers in scientific notation with adjustable precision, but does not strip trailing zeros.

I’m wondering if there’s already some library function out there that meets these criteria. I guess the only stumbling block for writing this myself is having to strip the trailing zeros from the significand in scientific notation produced by %G.

  • 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-26T18:27:02+00:00Added an answer on May 26, 2026 at 6:27 pm

    Have you looked at DecimalFormat yet?

    (what might also be of interest: BigDecimal#toString())

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

Sidebar

Related Questions

I am looking for a way to set date format in either mm/dd/yyyy or
I'm looking for a way to format numbers using tokens. This needs to be
I'm looking for a regex or a way to format the NumberValidator so that
I'm looking for way to format the XML syntax displayed in a pre tag
I'm looking for a way to format (as in whitespace, newlines where suitable) a
I am looking for any way to have Emacs format a Python buffer by
I am looking for a way to convert datetime objects to decimal(/float) year, including
I'm looking for a simple way to decode data stored in the Decimal64 format
In PHP (using built-in functions) I'd like to convert/format a number with decimal, so
I'm looking for a way to automatically format and color code I write in

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.