Dav*_*ang 285
String.format("%1$,.2f", myDouble);
Run Code Online (Sandbox Code Playgroud)
String.format 自动使用默认语言环境.
小智 39
String.format("%4.3f" , x) ;
Run Code Online (Sandbox Code Playgroud)
这意味着我们在ans中需要总共4位数,其中3位应该在十进制之后.f是double的格式说明符.x表示我们想要找到它的变量.为我工作...
mdr*_*drg 24
如果要使用手动设置的符号对其进行格式化,请使用:
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
Run Code Online (Sandbox Code Playgroud)
但是,基于区域设置的格式是首选.
Gur*_*oca 18
从此链接提取的代码;
Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
amountOut = numberFormatter.format(amount);
System.out.println(amountOut + " " +
currentLocale.toString());
Run Code Online (Sandbox Code Playgroud)
此示例的输出显示了相同数字的格式如何随Locale而变化:
345 987,246 fr_FR
345.987,246 de_DE
345,987.246 en_US
Run Code Online (Sandbox Code Playgroud)
Tor*_*res 13
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);
}
}
Run Code Online (Sandbox Code Playgroud)
打印出来:
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
| 归档时间: |
|
| 查看次数: |
337165 次 |
| 最近记录: |