มีวิธีจัดรูปแบบทศนิยมดังนี้:
100   -> "100"  
100.1 -> "100.10"
ถ้าเป็นตัวเลขกลมๆให้เว้นส่วนทศนิยม มิฉะนั้นจะจัดรูปแบบด้วยทศนิยมสองตำแหน่ง
มีวิธีจัดรูปแบบทศนิยมดังนี้:
100   -> "100"  
100.1 -> "100.10"
ถ้าเป็นตัวเลขกลมๆให้เว้นส่วนทศนิยม มิฉะนั้นจะจัดรูปแบบด้วยทศนิยมสองตำแหน่ง
คำตอบ:
ฉันสงสัยมัน. ปัญหาคือ 100 จะไม่ 100 ถ้ามันเป็นโฟลตปกติ 99.9999999999 หรือ 100.0000001 หรืออะไรทำนองนั้น
หากคุณต้องการจัดรูปแบบด้วยวิธีนี้คุณต้องกำหนด epsilon นั่นคือระยะห่างสูงสุดจากจำนวนเต็มและใช้การจัดรูปแบบจำนวนเต็มหากความแตกต่างมีค่าน้อยกว่าและเป็นทศนิยมอย่างอื่น
สิ่งนี้จะทำเคล็ดลับ:
public String formatDecimal(float number) {
  float epsilon = 0.004f; // 4 tenths of a cent
  if (Math.abs(Math.round(number) - number) < epsilon) {
     return String.format("%10.0f", number); // sdb
  } else {
     return String.format("%10.2f", number); // dj_segfault
  }
}
    ฉันขอแนะนำให้ใช้แพ็คเกจ java.text:
double money = 100.1;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println(moneyString);
สิ่งนี้มีประโยชน์เพิ่มเติมในการระบุตำแหน่งที่ตั้ง
แต่ถ้าคุณต้องการให้ตัดสตริงที่คุณได้รับกลับมาหากเป็นเงินทั้งดอลลาร์:
if (moneyString.endsWith(".00")) {
    int centsIndex = moneyString.lastIndexOf(".00");
    if (centsIndex != -1) {
        moneyString = moneyString.substring(1, centsIndex);
    }
}
    BigDecimalกับฟอร์แมตเตอร์  joda-money.sourceforge.netควรเป็นไลบรารีที่ดีที่จะใช้เมื่อเสร็จสิ้น
                    double amount =200.0;
Locale locale = new Locale("en", "US");      
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
System.out.println(currencyFormatter.format(amount));
หรือ
double amount =200.0;
System.out.println(NumberFormat.getCurrencyInstance(new Locale("en", "US"))
        .format(amount));
วิธีที่ดีที่สุดในการแสดงสกุลเงิน
เอาต์พุต
$ 200.00
หากคุณไม่ต้องการใช้เครื่องหมายให้ใช้วิธีนี้
double amount = 200;
DecimalFormat twoPlaces = new DecimalFormat("0.00");
System.out.println(twoPlaces.format(amount));
200.00 น
นอกจากนี้ยังสามารถใช้ (ด้วยตัวคั่นหลักพัน)
double amount = 2000000;    
System.out.println(String.format("%,.2f", amount));          
2,000,000.00
ฉันไม่พบวิธีแก้ปัญหาที่ดีหลังจากค้นหาด้วย Google เพียงโพสต์วิธีแก้ปัญหาของฉันเพื่อให้ผู้อื่นอ้างอิง ใช้priceToStringเพื่อจัดรูปแบบเงิน
public static String priceWithDecimal (Double price) {
    DecimalFormat formatter = new DecimalFormat("###,###,###.00");
    return formatter.format(price);
}
public static String priceWithoutDecimal (Double price) {
    DecimalFormat formatter = new DecimalFormat("###,###,###.##");
    return formatter.format(price);
}
public static String priceToString(Double price) {
    String toShow = priceWithoutDecimal(price);
    if (toShow.indexOf(".") > 0) {
        return priceWithDecimal(price);
    } else {
        return priceWithoutDecimal(price);
    }
}
    "###,###,###.00"เพื่อ"###,###,##0.00"แสดง "0,00" เมื่อค่าเป็น "0"
                    String.format()เป็นกระดาษห่อหุ้มแบบคงที่ที่ดีสำหรับสิ่งนั้น ไม่แน่ใจว่าทำไมคุณถึงใช้ '10'
                    ฉันใช้อันนี้ (ใช้ StringUtils จาก commons-lang):
Double qty = 1.01;
String res = String.format(Locale.GERMANY, "%.2f", qty);
String fmt = StringUtils.removeEnd(res, ",00");
คุณต้องดูแลเฉพาะสถานที่และสตริงที่เกี่ยวข้องที่จะสับเท่านั้น
ฉันคิดว่านี่เป็นเรื่องง่ายและชัดเจนสำหรับการพิมพ์สกุลเงิน:
DecimalFormat df = new DecimalFormat("$###,###.##"); // or pattern "###,###.##$"
System.out.println(df.format(12345.678));
ผลผลิต: $ 12,345.68
และหนึ่งในทางออกที่เป็นไปได้สำหรับคำถาม:
public static void twoDecimalsOrOmit(double d) {
    System.out.println(new DecimalFormat(d%1 == 0 ? "###.##" : "###.00").format(d));
}
twoDecimalsOrOmit((double) 100);
twoDecimalsOrOmit(100.1);
เอาท์พุต:
100
100.10
โดยปกติเราจะต้องทำการผกผันหากฟิลด์เงิน json ของคุณเป็นแบบโฟลตอาจเป็น 3.1, 3.15 หรือเพียงแค่ 3
ในกรณีนี้คุณอาจต้องปัดเศษเพื่อการแสดงผลที่เหมาะสม (และเพื่อให้สามารถใช้มาสก์บนช่องป้อนข้อมูลได้ในภายหลัง):
floatvalue = 200.0; // it may be 200, 200.3 or 200.37, BigDecimal will take care
Locale locale = new Locale("en", "US");      
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
BigDecimal valueAsBD = BigDecimal.valueOf(value);
    valueAsBD.setScale(2, BigDecimal.ROUND_HALF_UP); // add digits to match .00 pattern
System.out.println(currencyFormatter.format(amount));
    วิธีที่ดีที่สุดในการทำเช่นนั้น
    public static String formatCurrency(String amount) {
        DecimalFormat formatter = new DecimalFormat("###,###,##0.00");
        return formatter.format(Double.parseDouble(amount));
    }
100 -> "100.00" 
100.1 -> "100.10"
NumberFormat currency = NumberFormat.getCurrencyInstance();
String myCurrency = currency.format(123.5);
System.out.println(myCurrency);
เอาต์พุต:
$123.50
หากคุณต้องการเปลี่ยนสกุลเงิน
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.CHINA);
String myCurrency = currency.format(123.5);
System.out.println(myCurrency);
เอาต์พุต:
¥123.50
    คุณควรทำสิ่งนี้:
public static void main(String[] args) {
    double d1 = 100d;
    double d2 = 100.1d;
    print(d1);
    print(d2);
}
private static void print(double d) {
    String s = null;
    if (Math.round(d) != d) {
        s = String.format("%.2f", d);
    } else {
        s = String.format("%.0f", d);
    }
    System.out.println(s);
}
ซึ่งพิมพ์:
100
100,10
ฉันรู้ว่านี่เป็นคำถามเก่า แต่ ...
import java.text.*;
public class FormatCurrency
{
    public static void main(String[] args)
    {
        double price = 123.4567;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(price));
    }
}
    คุณสามารถทำอะไรแบบนี้และส่งจำนวนเต็มแล้วจึงเซ็นต์ตามหลัง
String.format("$%,d.%02d",wholeNum,change);
    ฉันเห็นด้วยกับ @duffymo ว่าคุณต้องใช้java.text.NumberFormatวิธีการนี้ คุณสามารถจัดรูปแบบทั้งหมดได้โดยไม่ต้องใช้ String ใด ๆ เปรียบเทียบตัวเอง:
private String formatPrice(final double priceAsDouble) 
{
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    if (Math.round(priceAsDouble * 100) % 100 == 0) {
        formatter.setMaximumFractionDigits(0);
    }
    return formatter.format(priceAsDouble);
}
สองบิตที่จะชี้ให้เห็น:
Math.round(priceAsDouble * 100) % 100เป็นเพียงการแก้ไขความไม่ถูกต้องของคู่ผสม / โฟลต โดยพื้นฐานแล้วเพียงแค่ตรวจสอบว่าเราปัดไปที่ตำแหน่งหลักร้อย (อาจเป็นอคติของสหรัฐฯ) มีเซนต์เหลืออยู่หรือไม่  setMaximumFractionDigits()วิธีการไม่ว่าตรรกะของคุณจะเป็นอย่างไรในการพิจารณาว่าทศนิยมควรถูกตัดทอนหรือsetMaximumFractionDigits()ไม่ควรใช้
*100และ%100ถูกสหรัฐลำเอียงคุณสามารถใช้Math.pow(10, formatter.getMaximumFractionDigits())แทนการกำหนดค่าตายตัว100ที่จะใช้หมายเลขที่ถูกต้องสำหรับสถานที่เกิดเหตุ ...
                    หากคุณต้องการทำงานเกี่ยวกับสกุลเงินคุณต้องใช้คลาส BigDecimal ปัญหาคือไม่มีวิธีการจัดเก็บตัวเลขลอยในหน่วยความจำ (เช่นคุณสามารถจัดเก็บ 5.3456 แต่ไม่ใช่ 5.3455) ซึ่งอาจส่งผลต่อการคำนวณที่ไม่ดี
มีบทความดีๆเกี่ยวกับการร่วมมือกับ BigDecimal และสกุลเงิน:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
รูปแบบตั้งแต่ 1000000.2 ถึง 1,000,000,20
private static final DecimalFormat DF = new DecimalFormat();
public static String toCurrency(Double d) {
    if (d == null || "".equals(d) || "NaN".equals(d)) {
        return " - ";
    }
    BigDecimal bd = new BigDecimal(d);
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
    DecimalFormatSymbols symbols = DF.getDecimalFormatSymbols();
    symbols.setGroupingSeparator(' ');
    String ret = DF.format(bd) + "";
    if (ret.indexOf(",") == -1) {
        ret += ",00";
    }
    if (ret.split(",")[1].length() != 2) {
        ret += "0";
    }
    return ret;
}
    โพสต์นี้ช่วยให้ฉันได้รับสิ่งที่ต้องการในที่สุด ดังนั้นฉันแค่อยากจะร่วมสนับสนุนรหัสของฉันที่นี่เพื่อช่วยเหลือผู้อื่น นี่คือรหัสของฉันพร้อมคำอธิบาย
รหัสต่อไปนี้:
double moneyWithDecimals = 5.50;
double moneyNoDecimals = 5.00;
System.out.println(jeroensFormat(moneyWithDecimals));
System.out.println(jeroensFormat(moneyNoDecimals));
จะกลับมา:
€ 5,-
€ 5,50
jeroensFormat () วิธีการจริง:
public String jeroensFormat(double money)//Wants to receive value of type double
{
        NumberFormat dutchFormat = NumberFormat.getCurrencyInstance();
        money = money;
        String twoDecimals = dutchFormat.format(money); //Format to string
        if(tweeDecimalen.matches(".*[.]...[,]00$")){
            String zeroDecimals = twoDecimals.substring(0, twoDecimals.length() -3);
                return zeroDecimals;
        }
        if(twoDecimals.endsWith(",00")){
            String zeroDecimals = String.format("€ %.0f,-", money);
            return zeroDecimals; //Return with ,00 replaced to ,-
        }
        else{ //If endsWith != ,00 the actual twoDecimals string can be returned
            return twoDecimals;
        }
}
วิธี displayJeroensFormat ที่เรียกเมธอด jeroensFormat ()
    public void displayJeroensFormat()//@parameter double:
    {
        System.out.println(jeroensFormat(10.5)); //Example for two decimals
        System.out.println(jeroensFormat(10.95)); //Example for two decimals
        System.out.println(jeroensFormat(10.00)); //Example for zero decimals
        System.out.println(jeroensFormat(100.000)); //Example for zero decimals
    }
จะมีผลลัพธ์ดังนี้
€ 10,50
€ 10,95
€ 10,-
€ 100.000 (In Holland numbers bigger than € 999,- and wit no decimals don't have ,-)
รหัสนี้ใช้สกุลเงินปัจจุบันของคุณ ในกรณีของฉันคือฮอลแลนด์ดังนั้นสตริงที่จัดรูปแบบสำหรับฉันจะแตกต่างจากคนในสหรัฐอเมริกา
เพียงแค่ดูอักขระ 3 ตัวสุดท้ายของตัวเลขเหล่านั้น รหัสของฉันมีคำสั่ง if เพื่อตรวจสอบว่าอักขระ 3 ตัวสุดท้ายเท่ากับ ", 00" หรือไม่ หากต้องการใช้สิ่งนี้ในสหรัฐอเมริกาคุณอาจต้องเปลี่ยนเป็น ".00" หากยังไม่ได้ผล
ฉันบ้าพอที่จะเขียนฟังก์ชันของตัวเอง:
สิ่งนี้จะแปลงจำนวนเต็มเป็นรูปแบบสกุลเงิน (สามารถแก้ไขทศนิยมได้เช่นกัน):
 String getCurrencyFormat(int v){
        String toReturn = "";
        String s =  String.valueOf(v);
        int length = s.length();
        for(int i = length; i >0 ; --i){
            toReturn += s.charAt(i - 1);
            if((i - length - 1) % 3 == 0 && i != 1) toReturn += ',';
        }
        return "$" + new StringBuilder(toReturn).reverse().toString();
    }
      public static String formatPrice(double value) {
        DecimalFormat formatter;
        if (value<=99999)
          formatter = new DecimalFormat("###,###,##0.00");
        else
            formatter = new DecimalFormat("#,##,##,###.00");
        return formatter.format(value);
    }
    สำหรับผู้ที่ต้องการจัดรูปแบบสกุลเงิน แต่ไม่ต้องการให้ขึ้นอยู่กับสกุลเงินท้องถิ่นเราสามารถทำได้:
val numberFormat = NumberFormat.getCurrencyInstance() // Default local currency
val currency = Currency.getInstance("USD")            // This make the format not locale specific 
numberFormat.setCurrency(currency)
...use the formator as you want...
    นี่คือสิ่งที่ฉันทำโดยใช้จำนวนเต็มแทนจำนวนเงินเป็นเซ็นต์แทน:
public static String format(int moneyInCents) {
    String format;
    Number value;
    if (moneyInCents % 100 == 0) {
        format = "%d";
        value = moneyInCents / 100;
    } else {
        format = "%.2f";
        value = moneyInCents / 100.0;
    }
    return String.format(Locale.US, format, value);
}
ปัญหาเกี่ยวกับ NumberFormat.getCurrencyInstance()คือบางครั้งคุณต้องการให้ $ 20 เป็น $ 20 มันดูดีกว่า $ 20.00
หากใครพบวิธีที่ดีกว่านี้โดยใช้ NumberFormat ฉันก็หูผึ่ง