Skip to content

Why does Money strip trailing zeros? #438

@joao-loureiro-ig

Description

@joao-loureiro-ig

I would like to ask why org.javamoney.moneta.Money chooses to strip trailing zeros in its internal BigDecimal representation of the numeric value of the amount. This loss of scale happens here: https://github.com/JavaMoney/jsr354-ri/blob/1.4.5/moneta-core/src/main/java/org/javamoney/moneta/spi/ConvertBigDecimal.java#L72.

The reason I ask is because it prevents me from using MonetaryAmount in common use cases, for which it would otherwise be an appropriate data type to use, with all the advantages that the JavaMoney API provides. (I notice that FastMoney and RoundedMoney appear to have the same behaviour. Of course, I could provide my own implementation of MonetaryAmount, but that defeats most of the point of using a proven, industry-grade existing implementation.)

Therefore, I would also like to ask if there is any solution or workaround in the Reference Implementation for preserving original scale.

To be more illustrative about these use cases:

They revolve around the fact that, sometimes, the scale (or precision) of the numeric value is important information about the monetary amount - it carries semantic meaning. Therefore, it needs to be preserved. This information is often not attached to the monetary amount, so a conversion to Money means that it is lost.

For example, a common scenario in financial applications is that an application connects to a source of quotes for financial instruments. This quoting system may transmit (for example) a quote of 1.420 with a known currency of USD for a given financial instrument. The trailing zero is relevant because it is implied that the instrument can be traded in fractions of US cents. This information is lost if the quote is converted to 1.42, as it is the case when converted to a Money object. While this information may exist elsewhere, it may not be possible, practical or desirable for the application to consume it separately, especially when it is already implicitly encoded in the original scale of the quote. I notice that this use case seems to fall under the Trading Site scenario considered by the Specification.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions