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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:50:04+00:00 2026-05-31T11:50:04+00:00

I’ve done some searching and I cannot determine how to do what I am

  • 0

I’ve done some searching and I cannot determine how to do what I am looking to do.

I have a method for rounding numbers which takes the paramaters numberToRound and decimalPlaces right now it is hardcoded to round to 4 decimal places but I need that to be dynamic. so if it is called with round(3.1415926536, 3) it would round to 3 decimal places rather than 4 but I cannot figure out how to get Java to take that 3 and understand that i want 000 to be printed or handled anyways.

any ideas on how to do this? Below is sample code that I have typed up but it is hard coded to 4 places since nothing I was trying would compile right

public class round
{
  public static void main(String[] args) 
  {
      System.out.println("pi rounded to 3 places is " + roundPI(3.1415926536, 3));
  }
  public static double roundPI(double numberToRound, int decimalPlaces)
  {

    numberToRound= (double)Math.round(numberToRound*10000)/10000;
    return numberToRound;
  }
}

current output

pi rounded to 3 places is 3.1416

I considered doing this with a math function of some kind like 10*100 to get to 1000 which would round to 3 but then if I need 4 or 5 decimal places or even 2 I still need it to figure out how to get to figure out what it needs to multiply by without hardcoding an if statement a mile long to handle if I change what im rounding to like an intrest calculator and a user decides they want it rounded to 10 or 12 or even more or less like 1 decimal place.

  • 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-31T11:50:05+00:00Added an answer on May 31, 2026 at 11:50 am
    powerOfTen    = Math.pow(10,decimalPlaces);
    numberToRound = (double)Math.round(numberToRound * powerOfTen) / powerOfTen;
    

    That will round to the correct number of places. But when you print, you also need to ensure that the right number of trailing zeroes are printed. For that, you can use System.out.printf.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.