Java: what is the correct way to convert a string to a valid amount (BigDecimal)
•
Java
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
二维码