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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:32:21+00:00 2026-06-10T01:32:21+00:00

In porting an algorithm from JavaScript to Java, I’ve run into the problem that

  • 0

In porting an algorithm from JavaScript to Java, I’ve run into the problem that I need a replacement for JavaScript’s toPrecision(). The problem is that I don’t have a clue how small or large the numbers will be, so I can’t use a simple NumberFormat with the right format.

Is there a standard class that offers a similar functionality?

EDIT
Here is what I came up with:

   double toPrecision(double n, double p) {
        if (n==0) return 0;

        double e = Math.floor(Math.log10(Math.abs(n)));
        double f = Math.exp((e-p+1)*Math.log(10));

        return Math.round(n/f)*f;
    }

In principle, it does the right thing, but rounding errors completely ruin it. For example,
toPrecision(12.34567, 3) returns 12.299999999999997

EDIT 2
This version works perfectly for 11 out of 12 test cases…

   double toPrecision(double n, double p) {
        if (n==0) return 0;

        double e = Math.floor(Math.log10(Math.abs(n)));
        double f = Math.round(Math.exp((Math.abs(e-p+1))*Math.log(10)));
        if (e-p+1<0) {
            f = 1/f;
        }

        return Math.round(n/f)*f;
    }

But toPrecision(0.00001234567, 3) still returns 1.2299999999999999E-5 instead of 1.23E-5

  • 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-10T01:32:23+00:00Added an answer on June 10, 2026 at 1:32 am

    Use BigDecimal and setScale() method to set the precision

    BigDecimal bd = new BigDecimal("1.23456789");
    System.out.println(bd.setScale(3,BigDecimal.ROUND_HALF_UP));
    

    Output

    1.235
    

    See

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

Sidebar

Related Questions

I am currently porting an application from iOS into Android and I have ran
I'm porting my application from WPF to Silverlight. The biggest problem is that my
I'm porting a complex algorithm from 32 Bit to 64 Bit that takes about
Porting an application from C# (1.1 framework) to VB.NET (3.5 framework), and I have
While porting over a code fragment from python I've stumbled over a trivial problem:
While porting data from one mysql database to another there have come some issues
I am porting an algorithm from C# to Ruby. This algorithm has one method
This algorithm is so advanced for my basic programming skills that I just don't
In porting a big app from a Windows to Linux, I need to be
A friend was in need of an algorithm that would let him loop through

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.