简体   繁体   中英

Convert String to BigDecimal

There are lots of solution for converting String to BigDecimal in Java but my question is little bit different

if (obj.getAmount() != null) {
    payment.setAmount(new BigDecimal(p.getAmount()));
}

How can I convert the above code in JDK 8..

I have tried below code but not working

AppCommonUtils.optional(payment.getAmount()).map(new::BigDecimal).ifPresent(txnCustomerPayment::setAmount);

public static Optional<String> optional(String value) {
   return StringUtils.isNotBlank(value) ? Optional.of(value) : Optional.empty();
}

There are few problems with your code eg new::BigDecimal is not a valid expression.

Assuming obj.getAmount() returns a String it should look like:

Optional.ofNullable(obj.getAmount())
    .map(BigDecimal::new)
    .ifPresent(payment::setAmount)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM