我正在尝试创建一个与现有 Java 函数等效的 VB.net。在此函数中,字符串被格式化为具有一定数量的小数和数字,但我无法弄清楚该函数到底在做什么。
for 循环使用给定 min/max
之间的数值填充 ArrayList。根据参数 dec2disp
,调整数值以显示一定数量的小数位。
Java代码如下:
for( double d = min; d <= max; d+=inc ) {
if((m_flags&dispZeroAsOff)!=0 && d==0){
vList.add(m_offString);
}else{
String fmt=String.format("%%.%df", dec2disp);
String temp = String.format(fmt, d);
vList.add(temp + unit);
}
}
让我感到困惑的是 String.format("%%.%df", dec2disp);
。我找到了 %d
和 %f
的解释,但找不到任何关于 %df
的内容。
最佳答案
I have found explanations for %d and %f, but cannot find anything anywhere about %df.
代表DecimalFormat
。
java.lang.Object
↳ java.text.Format
↳ java.text.NumberFormat
↳ java.text.DecimalFormat
A concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, or Indic digits. It also supports different flavors of numbers, including integers ("123"), fixed-point numbers ("123.4"), scientific notation ("1.23E4"), percentages ("12%"), and currency amounts ("$123"). All of these flavors can be easily localized.
引用 - DecimalFormat .
注意:
如果您对不同的字符串格式化程序感兴趣,请关注最后一个类 Formatter .
java.lang.Object
↳ java.util.Formatter
关于java - 格式化字符串时%df代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24613891/