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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:49:35+00:00 2026-06-06T08:49:35+00:00

I am thinking of using BigDecimal to compare two currency values rounded to 2

  • 0

I am thinking of using BigDecimal to compare two currency values rounded to 2 digits. For example I expect the following to yield 0 because I’m comparing something that should round to 0.02 with something that is 0.02. But it yields -1 !! Is there a right way to do this?

This should also work for larger numbers like 123.45 vs 123.4534 … which should yield 0 when “compareTo” is used.

I’ve tried using math context but it doesnt seem elegant … is there a correct way?

BigDecimal spread = new BigDecimal(0.01934);
spread.setScale(2, RoundingMode.HALF_EVEN);
System.out.format("0.0193 ? 0.02 = %d\n", spread.compareTo(new BigDecimal(0.02)));

AND THE SOLUTION IS:

BigDecimal spread = new BigDecimal(0.01934).setScale(2, RoundingMode.HALF_EVEN);
BigDecimal loSpread = new BigDecimal(0.02).setScale(2, RoundingMode.HALF_EVEN);
System.out.format("0.0193 ? 0.02 = %d\n", spread.compareTo(loSpread));
  • 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-06T08:49:38+00:00Added an answer on June 6, 2026 at 8:49 am

    0.02 gets evaluated to

    java.math.BigDecimal = 0.0200000000000000004163336342344337026588618755340576171875
    

    You have to setScale(2, RoundingMode.HALF_EVEN) there too.

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

Sidebar

Related Questions

I'm using Thinking Sphinx for full-text search, following this video . I'd like to
I am thinking about using the wired MAC-adr as a unique value in a
I am thinking about using Gallery. If I'm scrolling to the right, I want
I'm thinking about using the java application object to implement a simple cache, to
I was thinking of using a Double as the key to a HashMap but
I was thinking of using AUTONOMOUS_TRANSACTION Pragma for some logging in a batch process.
I'm thinking of using Google Docs as a content management system, and to integrate
I am thinking about using Gears in a project, but I have doubts about
I am thinking of using sed for reading .properties file, but was wondering if
I'm thinking about using a ViewFlipper for an Wizard like Activity. But I see

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.