There are many way you can do this. Those are given bellow:
Suppose your original number is given bellow:
double number = 2354548.235;
**Using `NumberFormat` and Rounding mode**
NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
DecimalFormat decimalFormatter = (DecimalFormat) nf;
decimalFormatter.applyPattern("#,###,###.##");
decimalFormatter.setRoundingMode(RoundingMode.CEILING);
String fString = decimalFormatter.format(number);
System.out.println(fString);
**Using String formatter**
System.out.println(String.format("%1$,.2f", number));
In all cases the **output** will be:
**2354548.24**
**Note**:
During rounding you can add `RoundingMode` in your formatter. Here are some **Rounding mode** given bellow:
decimalFormat.setRoundingMode(RoundingMode.CEILING);
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
decimalFormat.setRoundingMode(RoundingMode.UP);
**Here are the imports**:
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
There are many way you can do this. Those are given bellow:
Suppose your original number is given bellow:
double number = 2354548.235;
**Using NumberFormat:**
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(number));
**Using String.format:**
System.out.println(String.format("%,.2f", number));
**Using DecimalFormat and pattern:**
NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
DecimalFormat decimalFormatter = (DecimalFormat) nf;
decimalFormatter.applyPattern("#,###,###.##");
String fString = decimalFormatter.format(number);
System.out.println(fString);
**Using DecimalFormat and pattern**
DecimalFormat decimalFormat = new DecimalFormat("############.##");
BigDecimal formattedOutput = new BigDecimal(decimalFormat.format(number));
System.out.println(formattedOutput);
In all cases the **output** will be:
**2354548.23**
**Note**:
During rounding you can add `RoundingMode` in your formatter. Here are some rounding mode given bellow:
decimalFormat.setRoundingMode(RoundingMode.CEILING);
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
decimalFormat.setRoundingMode(RoundingMode.UP);
**Here are the imports**:
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;