Java: what is the correct way to convert a string to a valid amount (BigDecimal)

I must convert the incoming string field to a BigDecimal field representing a valid amount, for example:

String amount = "1000";

BigDecimal valid_amount = convert(amount);

print(valid_amount.toString())//1000.00

What API is used to convert a string to a valid amount in Java (for example, Apache commons Library)?

Thank you in advance,

Solution

How about the BigDecimal (string) constructor?

String amount = "1000";
BigDecimal validAmount = new BigDecimal(amount);
System.out.println(validAmount); // prints: 1000

If you want to format the output differently, use the formatter class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>