String.format() om dubbel te formatteren in Java

Hoe kan ik String.format(format, args)gebruiken om een double te formatteren zoals hieronder?

2354548.235-> 2,354,548.23


Antwoord 1, autoriteit 100%

String.format("%1$,.2f", myDouble);

String.formatgebruikt automatisch de standaard locale.


Antwoord 2, autoriteit 14%

String.format("%4.3f" , x) ;

Het betekent dat we in totaal 4 cijfers nodig hebben in ans , waarvan er 3 achter decimaal moeten staan.
En f is de formaatspecificatie van double . x betekent de variabele waarvoor we het willen vinden.
Werkte voor mij. . .


Antwoord 3, autoriteit 8%

Als je het wilt opmaken met handmatig ingestelde symbolen, gebruik dan dit:

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

Lokale opmaak heeft echter de voorkeur.


Antwoord 4, autoriteit 7%

code geëxtraheerd uit deze 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());

De uitvoer uit dit voorbeeld laat zien hoe het formaat van hetzelfde nummer varieert met locale:

345 987,246  fr_FR
345.987,246  de_DE
345,987.246  en_US

Antwoord 5, Autoriteit 4%

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);
  }
}

en print uit:

3 (3) +3 00003
Standaard Floating-Point-formaat: 1234567.123000
Drijvend punt met komma’s: 1.234.567.123000
Negatieve drijvende wijsaanduiding: -1.234.567.123000
Negatieve drijvende-puntoptie: (1.234.567.123000)

Lijn-up positieve en negatieve waarden:
1.234.567,12
-1.234.567,12


Antwoord 6

Er zijn veel manier om dit te doen. Die worden hieronder gegeven:

Stel dat uw origineel nummer hieronder wordt gegeven:
Dubbel nummer = 2354548.235;

met NumberFormaten afrondingsmodus

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);

String formatter gebruiken

System.out.println(String.format("%1$,.2f", number));

In alle gevallen zal de outputzijn:
2354548.24

Opmerking:

Tijdens het afronden kun je RoundingModetoevoegen aan je formatter. Hier zijn enkele Afrondingsmodidie hieronder worden gegeven:

   decimalFormat.setRoundingMode(RoundingMode.CEILING);
    decimalFormat.setRoundingMode(RoundingMode.FLOOR);
    decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    decimalFormat.setRoundingMode(RoundingMode.UP);

Dit zijn de importen:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

Antwoord 7

Gebruik DecimalFormat

NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
 DecimalFormat decimalFormatter = (DecimalFormat) nf;
 decimalFormatter.applyPattern("#,###,###.##");
 String fString = decimalFormatter.format(myDouble);
 System.out.println(fString);

Other episodes