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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:38:23+00:00 2026-06-04T15:38:23+00:00

I am writing a function round: static float round(float number, precision){} The function should

  • 0

I am writing a function round:
static float round(float number, precision){}

The function should work like this: round(12.3456f, 3) = 12.345

My definition of function is like this:

public static float round(float value, int precision) {
float result;
if(precision <= 0){
    throw new RuntimeException("Precision can not be zero or less");
}

int number = (int) power(10,precision);
value = value * number;
result = (float)Math.round(value)/number;

return result;
} 

But the issue is that, my unit test case for this function doesn’t pass,

 public void mathTestNew() {
    assertEquals("MathTest",12.341,OOTBFunctions.round(12.3416f,3));
 }

The result is
junit.framework.AssertionFailedError: MathTest expected:<12.341> but was:<12.342>

I am not sure how to overcome this error. I am not sure if BigDecimal will help me in this.

  • 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-04T15:38:25+00:00Added an answer on June 4, 2026 at 3:38 pm

    If you did want to use BigDecimal:

    public static float round(float value, int precision) {
        if (precision <= 0) {
            throw new IllegalArgumentException("Precision cannot be zero or less.");
        }
        BigDecimal decimal = BigDecimal.valueOf(value);
        return decimal.setScale(precision, RoundingMode.FLOOR).floatValue();
    }
    

    You may lose accuracy when converting from BigDecimal to float, so if accuracy is a must, do not convert; keep the value as a BigDecimal.

    As mentioned in other answers, float is an approximation of a base 10 number. The following demonstrates just that:

    System.out.println(BigDecimal.valueOf(12.3416f)); // outputs 12.34160041809082
    System.out.println(new BigDecimal("12.3416"));    // outputs 12.3416
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a function like this. func :: IO() func = putStr print
Writing a function I must declare input and output data types like this: int
I am writing a function to extract decimals from a number. Ignore the exception
I was writing a function for rounding a number to two places. And I
Can anyone help me with why this JavaScript is not writing to the definition
I have this response.write on my page function roundthecon() { document.write(Math.round(exchRate*Math.pow(10,2))/Math.pow(10,2)); } But it
I'm writing a function that is mostly static in nature. I want to plug
Hallo, I'm having trouble writing a function: float turnToRequestedHeading(float initialHeading, float requiredHeading, float turnRate)
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
While writing a function which will perform some operation with each number in a

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.