Format BigDecimal to Currency and Percentage
import java.math.BigDecimal;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
double subtotal = 123.123;
BigDecimal decimalSubtotal = new BigDecimal(Double.toString(subtotal));
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String message = currency.format(decimalSubtotal);
System.out.println(message);
message = percent.format(decimalSubtotal);
System.out.println(message);
}
}
Output:
$123.12
12,312%
Home
Java Book
Runnable examples
Java Book
Runnable examples
BigDecimal:
- Create BigDecimal from long and String
- Add two BigDecimal together
- Divide BigDecimal from another BigDecimal
- Multiply one BigDecimal to another BigDecimal
- Negate a BigDecimal
- Subtract BigDecimal from another BigDecimal
- Truncate BigDecimal value
- Power a BigDecimal
- Round a BigDecimal(double) up
- Round a BigDecimal(double) down
- Format BigDecimal to Currency and Percentage