Long vs. BigDecimal: Which Is Best for Storing Money?
The article surveys ten community‑suggested approaches for representing monetary values in Java—ranging from using Long, BigDecimal, String, Protobuf, custom types, to following leadership or AI advice—highlighting each option’s rationale and trade‑offs such as precision, range, performance, and maintainability.
Problem
Question: which data type should be used for monetary amounts— Long or BigDecimal? The discussion assumes Java but the same choice appears in many languages.
Community proposals
Long
Interpretation: When the unit is expressed in cents there is no fractional part, so precision is not an issue. The range of long is sufficient for typical amounts.
BigDecimal
Interpretation: BigDecimal is designed for precise calculations and is widely used. Using long is considered less professional and less adaptable.
Long + BigDecimal
Interpretation: Use long for amounts and prices, and BigDecimal for exchange rates or fees that require more decimal places.
String
Interpretation: Any value can be stored as a String, but all parsing, validation and arithmetic logic must be implemented manually; this approach requires expertise.
Protobuf
Interpretation: Protobuf has no native BigDecimal. It can be represented as a string or a custom type, with a slight performance penalty.
Custom type
Interpretation: Architects should design a domain‑specific type that naturally reflects business requirements, improving understandability, extensibility, and maintainability.
Follow leadership
Interpretation: The choice may be dictated by management rather than technical factors; comply while protecting oneself.
AI assistance
Interpretation: Large language models can generate a complete answer quickly, illustrating modern productivity tools.
Economy (small values)
Interpretation: For amounts of only a few hundred, using int, short, or even byte is sufficient; long is overkill.
Random suggestion
Interpretation: No technical reasoning provided; the suggestion is whimsical.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
SpringMeng
Focused on software development, sharing source code and tutorials for various systems.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
