How can I use String.format(format, args) to format a double like below?
2354548.235 -> 2,354,548.23
String.format("%1$,.2f", myDouble);
String.format automatically uses the default locale.
String.format(Locale.GERMAN, "%1$,.2f", myDouble);%1, %2 and so on can be used to re-order the output based on the index of your input arguments. See this. You can omit the index and the default order will be assumed by the formatter.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 . . .
String.format("%014.3f" , 58.656565) -> 0000000058,657: 14 symbols in total: 8 zeroes and 5 numbers and 1 comma. So in this case number 14 means the total number of symbols (extra ones get replaced with leading zero, could go without, then it would replace with space) in the result string. The .3 part is completely independent here.String.format("%014.3f" , 58.656565) -> 58,657. I take it because the preference goes to the .3f part: it first prints the number with 3 decimal places and after if there are symbols left it prints leading spaces (or zeroes/whatever). In this case the number is 2 + 3 + 1 = 6 symbols and we only wanted to print 4, so there are certainly no extra spaces.String.format("%014.3f" , 58.656565) two times, but the result is different, am I missing something?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;