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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:10:08+00:00 2026-05-23T17:10:08+00:00

Currently, I have BigDecimal values with 3 decimal places shown in a JTable. When

  • 0

Currently, I have BigDecimal values with 3 decimal places shown in a JTable. When i get values as 2.500 or 1.000, I want to have 2.5 and 1.0 instead. That’s why I tried this:

                    BigDecimal value;
                BigDecimal value3 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(3, BigDecimal.ROUND_HALF_UP);
                BigDecimal value2 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(2, BigDecimal.ROUND_HALF_UP);
                if(valor3.equals(value2))
                {
                    BigDecimal value1 = new BigDecimal((String)model.getValueAt(e.getLastRow(), 1)).setScale(1, BigDecimal.ROUND_HALF_UP);
                    if(value3.equals(value1))
                        value = valuer1;
                    else
                        value = value2;
                }
                else
                    value = value3;

But it doesn’t work. It looks like ‘2.500’.equals(‘2.5’) were false.

I also tried to give format to the JTable at its renderer:

 class paramRenderer extends DefaultTableCellRenderer
{
    private final DecimalFormat formatter = new DecimalFormat( "#.000" );

    public void setValue() {    
        formatter.setMinimumFractionDigits(1);
        formatter.setMaximumFractionDigits(3);
    }
}

But it makes no difference at all.

Any other idea?

EDIT: Finaly I found over the Internet a solution:

BigDecimal trim(BigDecimal n)
{
    try
    {
        while (true)
        {
            n = n.setScale(n.scale()-1);
        }
    }
    catch (ArithmeticException e)
    {
        // Not "real" error: No more trailing zeroes -> Just exit // setScale() tries to eliminate a non-zero digit -> Out of the loop // Remember exceptions are not recommended for exiting loops, but this seems to be the best way...
    }

    return n;
}

And works great! Thank you very much for the help anyway 🙂

  • 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-23T17:10:09+00:00Added an answer on May 23, 2026 at 5:10 pm
    • the first comparison is false, because you have different scales – value2 has 2 and value3 has 3. If you make them the same, it works
    • you don’t need BigDecimal if you only want to display things. The decimal format should work, but you should also set the maximum fraction digits to 1
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have latitude and longitude values in my database out to 10 decimal places:
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have a DetailsView in ASP.NET that gets data from the database based
We currently have a 10 year old nasty, spaghetti-code-style SQL Server database that we
I currently have an iPhone app that reads data from an external XML file
I currently have an NSView that draws a grid pattern (essentially a guide of
I currently have a game that takes in large images, easily bigger than 1MB,
I currently have a implementation where some markers coming from JSON list is shown,
Currently have this to get a value from the registry in TSQL. However, I
Currently have a form with a ListView. Users of role A get an image

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.