2

Rounding amounts in Germany we must use Math.round instead of Math.floor.

Is there any rounding-function for amounts who accepts the Locale in Java?

This is not improbably since toUpperCase respects the Locale too.

  • "we must use Math.round instead of Math.floor" Why? – Ben Aaronson Jun 4 '15 at 12:35
  • 2
    Rounding method is not language or culture dependent. – JacquesB Jun 4 '15 at 12:36
  • Using Math.round over Math.floor might depend on the country for the software that you're writing, but it most certainly doesn't depend on what country you're in in general. My advise is to make a factory that returns a lambda function (if you're using Java 8) based on Locale. – Neil Jun 4 '15 at 12:36
  • Rounding is different from truncating. Maybe different rounding modes are of different popularity in different countries, but I've never heard of anyone who says "round" to mean "truncate" (which is what floor does, really). – user7043 Jun 4 '15 at 12:39
  • 3
    @PeterRader The answer to your immediate question is no, there isn't a way to do this using standard java libraries. Would you prefer an answer explaining best approach to implementing one yourself? – Neil Jun 4 '15 at 12:46
7

Working at three different places in one state within the US, I've encountered three different ways of rounding numbers in different applications. This isn't locale dependent - its industry dependent (finance, retail, science all round differently for different reasons).

  • Reporting to the finance department (from my retail experience) wanted things rounded up (HALF_UP) always. Makes it consistent and easy to explain.
  • Retail wanted it rounded always in the customer's favor (this is not a default rounding mode). You may lose a penny here or there, but it reduced the likelihood of a person arguing with the front end manager about an incorrect receipt (which would cost much more than a penny)
  • Science used HALF_EVEN to avoid systematic biasing of the data (half the time x.5 gets rounded up, half the time it gets rounded down - so 1.5 gets rounded to 2, and 2.5 also gets rounded to 2 (more on this on Wikipedia which points out this is sometimes known as "bankers' rounding")).

A Locale object represents a specific geographical, political, or cultural region.

(From the javadocs for java.util.Locale)

So no. There is no local association for rounding. Not everyone in a given locale always rounds the same way. It depends on the application - not the geographical, political or cultural grouping.

  • If it depends the locale i could transfer a half cent one million times and earn 10k... i remember a movie about this hack. – Peter Rader Jun 4 '15 at 13:35
  • @PeterRader if you are dealing with money different things apply than if you are dealing with science papers. That distinction isn't covered in java.util.Locale. – user40980 Jun 4 '15 at 13:37
-1

"Kaufmännisches Runden" seem to just be the German name for "Bankers rounding". It is not something culture specific.

See the various rounding modes supported here: http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html

  • 1
    Putting part of that wikipedia page through google translate I get: "If the figure at the first dropped decimal place is a 0 , 1 , 2 , 3 or 4 , then rounded. If the figure at the first dropped decimal place a 5 , 6 , 7 , 8 or 9 , then it is rounded up ." So not even banker's rounding. Just round less than half down, half or more up. – Ben Aaronson Jun 4 '15 at 12:50
  • So the answer is no? It isn't implemented in Java standard libraries? – Neil Jun 4 '15 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.