I am writing a rest API. This has to return my object on GET method.
Now, when I request postman, I can get the balanceAmount:120.5. However, my requirement is to return the REST response with the field balanceAmount:121.00. Kind of number formatting and rounding up to the next 50.
Sample input-output:
120.1 => 120.50
120.9 => 121.00
120 => 120.00
I have to use the amount as double and I cannot make it as a string. The database table has the column type as Numeric(10,2). I am using spring boot 2.6.x and Java 11.
Please suggest an easier way to format such fields.
My POJO is similar to:
class Customer {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "oid", nullable = false, updatable = false)
private Long oid;
@Column(name = "balanceAmount", length = 100)
private Double balanceAmount;
}
Controller:
@RestController
@RequestMapping("/customer")
class CustomerService {
@GetMapping("/oid/{oid}")
public Customer getCustomer(@PathVariable("oid") String oid) {
if (log.isDebugEnabled()) {
log.debug("oid{} ", oid);
}
return new Customer(oid, 120.5);
}
}