Java - BigDecimal?

Here are a few hints: Use BigDecimal for computations if you need the precision that it offers (Money values often need this) 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 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()) ).

Here are a few hints: Use BigDecimal for computations if you need the precision that it offers (Money values often need this). 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. 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()) ).

2 Agree especially on number 2. Keep the view (formatting for the display) separate from the model (BigDecimal). You can always write a custom java.text.

Format to handle your specific data type. – Steve Kuo Sep 1 '09 at 0:33 Don't you mean DecimalFormat, not NumberFormat? – Mk12 Sep 1 '09 at 1:22 @Mk12, you can use either.

DecimalFormat is to be used when you want more control over the formatting. It provides the applyPattern() method that NumberFormat does not. – Vineet Reynolds Sep 1 '09 at 1:38 Oh, I guess NumberFormat works too.

Just used getCurrencyInstance() and than setRoundMode to HALF_EVEN. – Mk12 Sep 1 '09 at 1:38 Yes, it is getCurrencyInstance(Locale locale) that will help in localization. – Vineet Reynolds Sep 1 '09 at 1:41.

I would recommend a little research on Money Pattern. Martin Fowler in his book Analysis pattern has covered this in more detail. Public class Money { private static final Currency USD = Currency.

GetInstance("USD"); private static final RoundingMode DEFAULT_ROUNDING = RoundingMode. HALF_EVEN; private BigDecimal amount; private Currency currency; public static Money dollars(BigDecimal amount) { return valueOf(amount, USD); } Money(BigDecimal amount, Currency currency) { this(amount, currency, DEFAULT_ROUNDING); } Money(BigDecimal amount, Currency currency, RoundingMode rounding) { this. Amount = amount; this.

Currency = currency; this. Amount = amount. SetScale(currency.

GetDefaultFractionDigits(), rounding); } public BigDecimal getAmount() { return amount; } public Currency getCurrency() { return currency; } @Override public String toString() { return getCurrency().getSymbol() + " " + getAmount(); } public String toString(Locale locale) { return getCurrency(). GetSymbol(locale) + " " + getAmount(); } } Coming to the usage: You would represent all monies using Money object as opposed to BigDecimal. Representing money as big decimal will mean that you will have the to format the money every where you display it.

Just imagine if the display standard changes. You will have to make the edits all over the place. Instead using the Money pattern you centralize the formatting of money to a single location.

Money price = Money. Dollars(38.28); System.out. Println(price).

1) If you are limited to the double precision, one reason to use BigDecimals is to realize operations with the BigDecimals created from the doubles. 2) The BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, while the double wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double 3) It should make no difference You should have no difficulties with the $ and precision.

One way to do it is using System.out.printf.

Use BigDecimal. SetScale(2, BigDecimal. ROUND_HALF_UP) when you want to round up to the 2 decimal points for cents.Be aware of rounding off error when you do calculations though.

You need to be consistent when you will be doing the rounding of money value. Either do the rounding right at the end just once after all calculations are done, or apply rounding to each value before doing any calculations. Which one to use would depend on your business requirement, but generally, I think doing rounding right at the end seems to make a better sense to me.

Use a String when you construct BigDecimal for money value. If you use double, it will have a trailing floating point values at the end. This is due to computer architecture regarding how double/float values are represented in binary format.

4 Note that for financial apps, ROUND_HALF_EVEN is the most common rounding mode since it avoids bias. – Michael Borgwardt Aug 31 '09 at 23:49 Good point about avoiding bias. Didn't know it worked to achieve that.

– Vineet Reynolds Aug 31 '09 at 23:54 @Michael: Thanks for the tip. I didn't know that. I always wondered how best deal with bias/cumulative errors.

Learned something new today. :) – tim_wonil Sep 1 '09 at 0:23.

There is an extensive example of how to do this on javapractices.com. See in particular the Money class, which is meant to make monetary calculations simpler than using BigDecimal directly. The design of this Money class is intended to make expressions more natural.

For example: if ( amount. Lt(hundred) ) { cost = amount. Times(price); } The WEB4J tool has a similar class, called Decimal, which is a bit more polished than the Money class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions