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

  • Home
  • SEARCH
  • 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 781247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:12:11+00:00 2026-05-14T20:12:11+00:00

If the value is 200.3456 , it should be formatted to 200.34 . If

  • 0

If the value is 200.3456, it should be formatted to 200.34.
If it is 200, then it should be 200.00.

  • 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-14T20:12:11+00:00Added an answer on May 14, 2026 at 8:12 pm

    Here’s an utility that rounds (instead of truncating) a double to specified number of decimal places.

    For example:

    round(200.3456, 2); // returns 200.35
    

    Original version; watch out with this

    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;
    }
    

    This breaks down badly in corner cases with either a very high number of decimal places (e.g. round(1000.0d, 17)) or large integer part (e.g. round(90080070060.1d, 9)). Thanks to Sloin for pointing this out.

    I’ve been using the above to round “not-too-big” doubles to 2 or 3 decimal places happily for years (for example to clean up time in seconds for logging purposes: 27.987654321987 -> 27.99). But I guess it’s best to avoid it, since more reliable ways are readily available, with cleaner code too.

    So, use this instead

    (Adapted from this answer by Louis Wasserman and this one by Sean Owen.)

    public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();
    
        BigDecimal bd = BigDecimal.valueOf(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
    

    Note that HALF_UP is the rounding mode “commonly taught at school”. Peruse the RoundingMode documentation, if you suspect you need something else such as Bankers’ Rounding.

    Of course, if you prefer, you can inline the above into a one-liner:
    new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

    And in every case

    Always remember that floating point representations using float and double are inexact.
    For example, consider these expressions:

    999199.1231231235 == 999199.1231231236 // true
    1.03 - 0.41 // 0.6200000000000001
    

    For exactness, you want to use BigDecimal. And while at it, use the constructor that takes a String, never the one taking double. For instance, try executing this:

    System.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
    System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));
    

    Some excellent further reading on the topic:

    • Item 48: “Avoid float and double if exact answers are required” in Effective Java (2nd ed) by Joshua Bloch
    • What Every Programmer Should Know About Floating-Point Arithmetic

    If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

    Specifically, note that round(200, 0) returns 200.0. If you want to output “200.00“, you should first round and then format the result for output (which is perfectly explained in Jesper’s answer).

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

Sidebar

Related Questions

Using Crystal Report 7 report id value 01 100 02 200 01 100 04
Using Access 2003 Table1 ID Name Value 001 Raja 100 002 Ramu 200 004
I want to add the lable box value in vb6 Label1 = 200 Label2
Using Crystal Report 7 ID Value 001 100 002 200 003 400 004 500
I need to turn the following results... RowID ColumnName Value ======= ============ ========== 200
When I add the value from database, the listbox should avoid the duplicate value.
I have a table with this values: ID VALUE ----------------------- 23559 200 23562 -1
I want the value To=200.0 will be equal to the grid size automaticaly <ListBox
our $TEST; *TEST = \100; $TEST =200 I want to change TEST 's value
in my user.data.crop_position value is [ 100, 100, 200, 200 ]; var crop_position=user.data.crop_position.slice(1,user.data.crop_position.length-2); $('#cropbox').Jcrop({

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.