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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:45:34+00:00 2026-05-12T11:45:34+00:00

I was trying to make my own class for currencies using longs, but apparently

  • 0

I was trying to make my own class for currencies using longs, but apparently I should use BigDecimal instead. Could someone help me get started? What would be the best way to use BigDecimals for dollar currencies, like making it at least but no more than 2 decimal places for the cents, etc. The API for BigDecimal is huge, and I don’t know which methods to use. Also, BigDecimal has better precision, but isn’t that all lost if it passes through a double? if I do new BigDecimal(24.99), how will it be different than using a double? Or should I use the constructor that uses a String instead?

  • 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-12T11:45:34+00:00Added an answer on May 12, 2026 at 11:45 am

    Here are a few hints:

    1. Use BigDecimal for computations if you need the precision that it offers (Money values often need this).
    2. Use the NumberFormat class for display. This class will take care of localization issues for amounts in different currencies. However, it will take in only primitives; therefore, if you can accept the small change in accuracy due to transformation to a double, you could use this class.
    3. When using the NumberFormat class, use the scale() method on the BigDecimal instance to set the precision and the rounding method.

    PS: In case you were wondering, BigDecimal is always better than double, when you have to represent money values in Java.

    PPS:

    Creating BigDecimal instances

    This is fairly simple since BigDecimal provides constructors to take in primitive values, and String objects. You could use those, preferably the one taking the String object. For example,

    BigDecimal modelVal = new BigDecimal("24.455");
    BigDecimal displayVal = modelVal.setScale(2, RoundingMode.HALF_EVEN);
    

    Displaying BigDecimal instances

    You could use the setMinimumFractionDigits and setMaximumFractionDigits method calls to restrict the amount of data being displayed.

    NumberFormat usdCostFormat = NumberFormat.getCurrencyInstance(Locale.US);
    usdCostFormat.setMinimumFractionDigits( 1 );
    usdCostFormat.setMaximumFractionDigits( 2 );
    System.out.println( usdCostFormat.format(displayVal.doubleValue()) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make my own Jabber bot but i have run into
I've been trying to make my own basic .obj (Wavefront) renderer using the OpenGL
I'm trying to make an ActivityGroup which has it's own interface, which should be
i'm trying to make my own IAuthorizationFilter attribute class. Basically, each api call has
Ok, so I'm trying to make my own markup language [for a class, I
I'm trying to make my own control in for .NET using VB and I'm
I'm trying to make my own simple framework in PHP. It's going OK but
I'm having a problem while trying to make my own chess game using JLayeredPane
I'm learning about generics, so I'm trying to make my own generic array class,
I'm trying to make Core Data objects inherit from my own custom class rather

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.