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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:48:31+00:00 2026-06-15T13:48:31+00:00

I was solving a problem which requires the output to be truncated and to

  • 0

I was solving a problem which requires the output to be truncated and to my surprise I haven’t been able to find a way to truncate numbers in java.

The output needs to be the a number followed by 6 decimal places.

What I want is
double truncate(double number,int places) and the output to be
truncate(14/3.0) = 4.666666.

but what I’m getting is

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
  }
// rounds( 14/3.0 , 6 ) = 4.666667

With String.format I get

String.format("%.6f", 14/3.0) = 4.666667

I also tried a solution that I found on stackoverflow that suggested using BigDecimal and that gave me the very same answer.

NumberFormat also seems to work in the same way

java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(6);
System.out.println(f.format(14/3.0)); // 4.666667

Is there a built in method that does this or do I need to write my own using something like BigInteger?
Is there something I’m doing wrong?

  • 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-15T13:48:32+00:00Added an answer on June 15, 2026 at 1:48 pm

    It’s because you are not truncating but rounding:

    long tmp = Math.round(value);
    

    If you use a cast instead it should work:

    long tmp = (long) value;
    

    So the code below outputs 4.666666 as you expect:

    public static void main(String[] args) throws Exception {
        System.out.println(truncate(14/3.0, 6));
    }
    
    public static double truncate(double value, int places) {
        if (places < 0) {
            throw new IllegalArgumentException();
        }
    
        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = (long) value;
        return (double) tmp / factor;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm solving a problem which requires very fast input/output. More precisely, the input data
I am solving a quantum-mech problem which requires me to find some eigenvalues by
I've been problem solving this for the past few hours and frustratingly can't find
I am in the middle of solving a problem which requires me to do
I have this problem which requires solving for X in AX=B . A is
i had a problem and solving which required a functionality built in mootools .
I do scientific image processing in MATLAB, which requires solving weird optimization problems with
I'm solving a problem which has some time and memory constraints, and unfortunately this
I am solving problem 9 on the Project Euler . In my solution I
I am solving the problem in the interview question about LinkedList. question is... Write

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.