ฉันจะใช้ String.format (format String, X) เพื่อจัดรูปแบบ double ดังนี้ได้อย่างไร
2354548.235 -> 2,354,548.23
ขอบคุณ!
ฉันจะใช้ String.format (format String, X) เพื่อจัดรูปแบบ double ดังนี้ได้อย่างไร
2354548.235 -> 2,354,548.23
ขอบคุณ!
คำตอบ:
String.format("%1$,.2f", myDouble);
String.format ใช้ภาษาเริ่มต้นโดยอัตโนมัติ
%1, %2และอื่น ๆ สามารถใช้เพื่อจัดเรียงเอาต์พุตใหม่ตามดัชนีของอาร์กิวเมนต์อินพุตของคุณ ดูนี่สิ คุณสามารถละเว้นดัชนีและลำดับเริ่มต้นจะถูกสันนิษฐานโดยฟอร์แมตเตอร์
String.format("%4.3f" , x) ;
มันหมายความว่าเราต้องการตัวเลข 4 หลักใน ans โดยที่ 3 ควรเป็นทศนิยม และ f คือตัวระบุรูปแบบของ double x หมายถึงตัวแปรที่เราต้องการค้นหา ทำงานให้ฉัน . .
String.format("%014.3f" , 58.656565)-> 0000000058,657: 14 สัญลักษณ์ทั้งหมด: 8 เลขศูนย์และ 5 หมายเลขและ 1 เครื่องหมายจุลภาค ดังนั้นในจำนวนกรณีนี้14 หมายถึงจำนวนของสัญลักษณ์ (คนพิเศษได้รับการแทนที่ด้วยศูนย์นำอาจจะไปโดยไม่ต้องแล้วก็จะเข้ามาแทนที่ด้วยช่องว่าง) ในสตริงผล .3ส่วนหนึ่งเป็นอิสระอย่างสมบูรณ์ที่นี่
String.format("%014.3f" , 58.656565) 58,657ฉันรับไปเพราะการตั้งค่าไปที่ชิ้น.3fส่วน: ก่อนพิมพ์ตัวเลขที่มีทศนิยม 3 ตำแหน่งและหลังจากนั้นหากมีสัญลักษณ์เหลืออยู่มันจะพิมพ์ช่องว่างนำหน้า ในกรณีนี้จำนวนคือ 2 + 3 + 1 = 6 สัญลักษณ์และเราต้องการพิมพ์ 4 เท่านั้นดังนั้นจึงไม่มีช่องว่างเพิ่มเติม
หากคุณต้องการจัดรูปแบบด้วยสัญลักษณ์ตั้งค่าด้วยตนเองใช้สิ่งนี้:
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
การจัดรูปแบบตามสถานที่เป็นที่ต้องการ
รหัสสกัดจากลิงค์นี้ ;
Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
amountOut = numberFormatter.format(amount);
System.out.println(amountOut + " " +
currentLocale.toString());
ผลลัพธ์จากตัวอย่างนี้แสดงให้เห็นว่ารูปแบบของหมายเลขเดียวกันแตกต่างกันอย่างไรกับ 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);
}
}
และพิมพ์ออกมา:
3 (3) +3 00003
รูปแบบจุดลอยตัวเริ่มต้น: 1234567,123000
จุดลอยตัวด้วยเครื่องหมายจุลภาค: 1.234.567,123000
ค่าลบเริ่มต้น: -1.234.567,123000
ตัวเลือกจุดลอยตัวเชิงลบ: (1.234.567 123000)ค่าบวกและค่า
ติดตั้งต่างๆของไลน์: 1.234.567,12
-1.234.567,12
มีหลายวิธีที่คุณสามารถทำได้ เหล่านั้นจะได้รับร้อง:
สมมติว่าหมายเลขเดิมของคุณได้รับตะโกน: double number = 2354548.235;
การใช้NumberFormatและการปัดเศษโหมด
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);
การใช้ตัวจัดรูปแบบสตริง
System.out.println(String.format("%1$,.2f", number));
ในทุกกรณีผลลัพธ์จะเป็น: 2354548.24
หมายเหตุ :
ในระหว่างการปัดเศษคุณสามารถเพิ่มRoundingModeในฟอร์แมตเตอร์ของคุณ ต่อไปนี้เป็นโหมดการปัดเศษที่กำหนดให้ร้อง:
decimalFormat.setRoundingMode(RoundingMode.CEILING);
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
decimalFormat.setRoundingMode(RoundingMode.UP);
นี่คือการนำเข้า :
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
ใช้ DecimalFormat
NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
DecimalFormat decimalFormatter = (DecimalFormat) nf;
decimalFormatter.applyPattern("#,###,###.##");
String fString = decimalFormatter.format(myDouble);
System.out.println(fString);
String.format(Locale.GERMAN, "%1$,.2f", myDouble);