Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places. /* Code example to print a double to two decimal places with Java printf */ System.
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
String.format("%1$,.2f", myDouble);
String.format
automatically uses the default locale.
String.format("%4.3f" , x) ;
It means that we need total 4 digits in ans , of which 3 should be after decimal . And f is the format specifier of double . x means the variable for which we want to find it . Worked for me . . .
If you want to format it with manually set symbols, use this:
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
decimalFormatSymbols.setGroupingSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00", decimalFormatSymbols);
System.out.println(decimalFormat.format(1237516.2548)); //1,237,516.25
Locale-based formatting is preferred, though.
code extracted from this link ;
Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
amountOut = numberFormatter.format(amount);
System.out.println(amountOut + " " +
currentLocale.toString());
The output from this example shows how the format of the same number varies with Locale:
345 987,246 fr_FR
345.987,246 de_DE
345,987.246 en_US
public class MainClass {
public static void main(String args[]) {
System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3);
System.out.printf("Default floating-point format: %f\n", 1234567.123);
System.out.printf("Floating-point with commas: %,f\n", 1234567.123);
System.out.printf("Negative floating-point default: %,f\n", -1234567.123);
System.out.printf("Negative floating-point option: %,(f\n", -1234567.123);
System.out.printf("Line-up positive and negative values:\n");
System.out.printf("% ,.2f\n% ,.2f\n", 1234567.123, -1234567.123);
}
}
And print out:
3 (3) +3 00003
Default floating-point format: 1234567,123000
Floating-point with commas: 1.234.567,123000
Negative floating-point default: -1.234.567,123000
Negative floating-point option: (1.234.567,123000)Line-up positive and negative values:
1.234.567,12
-1.234.567,12
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With