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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:55:18+00:00 2026-06-09T05:55:18+00:00

I have a Double that I want to knock the extra digits after the

  • 0

I have a Double that I want to knock the extra digits after the decimal place off of (I’m not too concerned about accuracy but feel free to mention it in your answer) prior to conversion into a String.

I was wondering whether it would be better to cast to an int or to use a DecimalFormat and call format(..) . Also, is it then more efficient to specify String.valueOf() or leave it as it is and let the compiler figure it out?

Sorry if I sound a bit ignorant, I’m genuinely curious to learn more of the technical details.

For reference, i’m drawing text to and android canvas:

c.drawText("FPS: " + String.valueOf((int)lastFps), xPos, yPos, paint); 
  • 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-09T05:55:20+00:00Added an answer on June 9, 2026 at 5:55 am

    I made a program that used System.nanoTime() to calculate the execution time of these two methods:

        public static void cast() {
        for (int i=0; i<1000000; i++) {
            int x= (int)Math.random();
        }
    }
    
    public static void format() {
        for (int i=0; i< 1000000; i++) {
            DecimalFormat df = new DecimalFormat("#");
            df.format(Math.random());
        }
    }
    

    Here are the respective results:

    80984944

    6048075593

    Granted my tests probably aren’t perfect examples. I’m just using math.random(), which generates a number that will always cast to 0, which might affect results. However, these results do make sense – casting should be cheap, since it likely doesn’t operate on the bits at all – the JVM just treats the bits differently.

    Edit: If I pull out the instantiation of the formatter for the second example, the program runs in 3155165182ns. If I multiply the random numbers by Integer.MAX_VALUE in both cases (with the instantiation pulled out), the results are: 82100170 and 4174558079. Looks like casting is the way to go.

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

Sidebar

Related Questions

I have a function fun that returns a double. I want to call the
I have a column in my database that is typed double and I want
I have a text file that have strings enclosed within double-quotes. I want to
Say I have a number, double, or NSNumber, 3.333333 I want that to turn
I have a double 2D image that I want to use in my UI
I have a method that i want to return as a double with 8
I have this method (Matrix::WriteToArray(double &CopyOfArray)) that I want to write a copy of
I have several UITextFields. I want to disable the double tap that brings the
I have a span element that I want to become editable upon double-click. (That
How to get the double value that is only two digit after decimal point.

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.