I need to convert an incoming string field to a BigDecimal field that will represent the actual amount of money, for example:
String amount = "1000"; BigDecimal valid_amount = convert(amount); print(valid_amount.toString())//1000.00
What is the correct API to use converting strings to real money in Java (ex: apache commons library)?
Thanks in advance,
How about a constructor BigDecimal(String)?
BigDecimal(String)
String amount = "1000"; BigDecimal validAmount = new BigDecimal(amount); System.out.println(validAmount); // prints: 1000
If you want to format the output in different ways, use the class Formatter.
Formatter
You wanted to achieve the following :?
NumberFormat nf = NumberFormat.getCurrencyInstance(); System.out.println(nf.format(new BigDecimal("1000")));
Output
$1,000.00
new BigDecimal(strValue).
new BigDecimal(strValue)
, The Evil BigDecimal Constructor
There is a Joda-Money library for working with monetary values. But, according to the website, "the current development release is for feedback, not for use in production."
If you want to print a decimal value using the setScale method
String amount = "1000"; BigDecimal validAmount = new BigDecimal(amount).setScale(2,RoundingMode.CEILING); System.out.println(validAmount); // prints: 1000.00
Source: https://habr.com/ru/post/1740385/More articles:Добавление новых функций в приложение с использованием поколения "на лету" - javaloading multiple flash files for php - javaAlternatives to VSTO - c #How to get coefficient in integer division in matlab? - matlabBizarre WHERE col = NULL behavior - nullВ T-SQL, как отображать столбцы для данного имени таблицы? - sql-serverHow to manage end-user documentation for a project with ongoing integration? - continuous-integrationPosting new in gcc - c ++org.hibernate.TransientObjectException during Criteria.list () - javaUsing a proxy template with C ++ iterators - c ++All Articles