Giter Club home page Giter Club logo

Comments (3)

niloc132 avatar niloc132 commented on July 16, 2024

I'm not understanding what you expect to happen here - when I run either line from a normal JVM (i.e. not GWT), I get an exception (tested in Java 11 and 21, line numbers below from 11):

Exception java.lang.ArithmeticException: Rounding necessary
      at BigDecimal.commonNeedIncrement (BigDecimal.java:4653)
      at BigDecimal.needIncrement (BigDecimal.java:4860)
      at BigDecimal.divideAndRound (BigDecimal.java:4835)
      at BigDecimal.setScale (BigDecimal.java:2923)
      at BigDecimal.setScale (BigDecimal.java:2838)

Are you sure that this should work as you have it written? Or are you saying that GWT incorrectly does not throw an exception here (though GWT's behavior appears to the same at a brief glance...)?

from gwt.

jnehlmeier avatar jnehlmeier commented on July 16, 2024

Well that's how Java has defined BigDecimal. If you want to reduce the scale you have to tell BigDecimal how to round the number in order to fit into the new scale.

Reducing the scale using RoundingMode.UNNECESSARY can only be successful if your BigDecimal has enough zeros at the end. If you want to truncate your BigDecimal to a given scale you should use RoundingMode.DOWN/FLOOR.

Also in your example you have used the constructor BigDecimal(double) which is kind of a beast as there are many double values that cannot be represented exactly in binary form. Given your example number 91.0202 one might think the scale is 4 but in reality it is 46 because 91.0202 cannot be represented exactly. The binary form of that number represents 91.0202000000000026602720026858150959014892578125.

So it is advisable to use the constructor BigDecimal(String) instead.

Using the string based constructor something like below will work:

new BigDecimal("92.0200").setScale(2, RoundingMode.UNNECESSARY);
new BigDecimal("92.0200").setScale(2);

It works because the number has two zeros at the end and thus fits into a scale of 2 without any rounding.

from gwt.

jnehlmeier avatar jnehlmeier commented on July 16, 2024

Closing the issue because the exception is expected based on the provided example.

from gwt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.