You can use the setScale() method to control the rounding of a decimal value as shown in the following example:
double value = 123.456789;
BigDecimal bd = new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(bd);
written by objects
\\ tags: BigDecimal, decimal, double, rounding
The BigDecimal constructors do not take the Locale into account when parsing number strings.
This means the following code will throw a NumberFormatException
Locale.setDefault(new Locale("nl", "NL"));
String s = "2.343.298,09324798";
BigDecimal bd = new BigDecimal(s);
To parse localized strings as BigDecimal we instead need to use the DecimalFormat class
Locale.setDefault(new Locale("nl", "NL"));
String s = "2.343.298,09324798";
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
df.setParseBigDecimal(true);
BigDecimal bd = (BigDecimal) df.parse(s);
written by objects
\\ tags: BigDecimal, DecimalFormat, Locale, NumberFormat, parse
The BigDecimal class makes this really easy as it has a method toByteArray() that returns exactly what we need. Just need to create a BigDecimal and call the method.
byte[] bytes = new BigInteger(hexString, 16).toByteArray();
written by objects
\\ tags: BigDecimal, byte array, hex