MongoDB – What about Decimal type of value?

If you want an exact representation for financial purposes, then doubles or floating point values are unsuitable as the fractional parts are subject to rounding error. Certain decimal values cannot not be represented using binary-based floating points and must be approximated. For a less technical intro, see The trouble with rounding floating point numbers; if … Read more

Best way to convert Locale specific String to BigDecimal

It seems like there is no other way since java.Lang.Number doesn’t have a method which returns a BigDecimal type. Anyway it makes sense because BigDecimal only accepts strings which are properly formatted not like “2.105,88” but like “2105.88”. Let me show your my code: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; public … Read more

Equals operator for zeros (BigDecimal / Double) in Java

BigDecimal ‘equals’ compares the value and the scale. If you only want to compare values (0 == 0.0) you should use compareTo: BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0.0)) == 0 //true BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0)) == 0 //true See the javadoc. As for the Double comparison, as explained by other answers, you are comparing a Double with an Integer in new Double(0.0).equals(0), which … Read more

How to display a number with always 2 decimal points using BigDecimal?

BigDecimal is immutable, any operation on it including setScale(2, BigDecimal.ROUND_HALF_UP) produces a new BigDecimal. Correct code should be BigDecimal bd = new BigDecimal(1); bd.setScale(2, BigDecimal.ROUND_HALF_UP); // this does change bd bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println(bd); output 1.00 Note – Since Java 9 BigDecimal.ROUND_HALF_UP has been deprecated and you should now use RoundingMode.ROUND_HALF_UP.

Rails NoMethodError: undefined method new for BigDecimal:Class

As per the documentation of BigDecimal, for all Ruby versions 2.6.x, you should be using BigDecimal with a specific version: The differences among versions are given below: version characteristics Supported ruby version range 2.0.0 You cannot use BigDecimal.new and do subclassing 2.4 .. 1.4.x BigDecimal.new and subclassing always prints warning. 2.3 .. 2.6 1.3.5 You … Read more

BigDecimal in JavaScript

Since we have native support for BigInt, it doesn’t require much code any more to implement BigDecimal. Here is a BigDecimal class based on BigInt with the following characteristics: The number of decimals is configured as a constant, applicable to all instances. Whether excessive digits are truncated or rounded is configured as a boolean constant. … Read more

Logarithm of a BigDecimal

Java Number Cruncher: The Java Programmer’s Guide to Numerical Computing provides a solution using Newton’s Method. Source code from the book is available here. The following has been taken from chapter 12.5 Big Decimal Functions (p330 & p331): /** * Compute the natural logarithm of x to a given scale, x > 0. */ public … Read more

Which rounding mode to be used for currency manipulation in java?

There is no “correct” mode, it depends on the business case. Examples: When calculating annual taxes, the fractions are often cut off (RoundingMode.FLOOR). When calculating a bonus, you might want to always round in favor of the customer (RoundingMode.CEILING). For taxes on a bill, you usually round HALF_UP When doing complex financial simulations, you don’t … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)