Dec 14

## How to format value as a percentage

When you need to format a number as a percent you can use the ‘%’ symbol in your DecimalFormat string. The static helper method getPercentInstance() can also be used if you don’t need complete control over the format string.

When the % symbol is used the value is first multiplied by 100 before applying the format string. So 0.123 would become 12.3%.

```
NumberFormat format1 = new DecimalFormat("##.####%");
NumberFormat format2 = NumberFormat.getPercentInstance();

String formatted1 = format1.format(value);
String formatted2 = format2.format(value);

```

written by objects \\ tags: , , , , , ,

Nov 15

## Creating BigDecimal from localized strings

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: , , , ,