Moshi – java.math.BigDecimal requires explicit JsonAdapter to be registered

Moshi does not have built-in adapters to process types like java.math.BigDecimal; we need to manually provide a custom adapter to handle the BigDecimal type. Table of contents: 1. Download Moshi 2. BigDecimal requires explicit JsonAdapter 3. Make Moshi supports BigDecimal 3.1 Define BigDecimal Adapter 3.2 Register the Adapter with Moshi 4. Download Source Code 5. …

Read more

Java 8 – How to Sum BigDecimal using Stream?

In Java 8, we can use the Stream.reduce() to sum a list of BigDecimal. 1. Stream.reduce() Java example to sum a list of BigDecimal values, using a normal for loop and a stream.reduce(). JavaBigDecimal.java package com.mkyong; import java.math.BigDecimal; import java.util.LinkedList; import java.util.List; public class JavaBigDecimal { public static void main(String[] args) { List<BigDecimal> invoices = …

Read more

Java – Display double in 2 decimal places

In Java, there are few ways to display double in 2 decimal places. Table of contents. 1. DecimalFormat("0.00") 2. String.format("%.2f") 3. BigDecimal 4. References Possible Duplicate How to round double / float value to 2 decimal places 1. DecimalFormat(“0.00”) We can use DecimalFormat("0.00") to ensure the number is round to 2 decimal places. DecimalExample.java package …

Read more

How to calculate monetary values in Java

There are many monetary values calculation in the financial or e-commerce application, and there is one question that arises for this – Should we use double or float data type to represent the monetary values? Answer: Always uses java.math.BigDecimal to represent the monetary values. 1. Double or Float? Here is an example of using double …

Read more