Java中如何用下划线填充数字?
如何在Java中用下划线填充数字,而不是通常的零?
例如,我希望
- 将 123.45 格式化为 ___123.45,
- 将 12345.67 格式化为 _12345.67
- 0.12 格式化为 ______.12
我尝试了很多东西,最接近的是这个(通过使用SYMBOLS.setZeroDigit('_');):
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class Example {
public static void main(String[] args) {
DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols();
SYMBOLS.setDecimalSeparator('.');
SYMBOLS.setGroupingSeparator(',');
DecimalFormat OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);
System.out.println(OUTPUT_FORMAT.format(0.01));
// got 000,000.01
System.out.println(OUTPUT_FORMAT.format(0.12));
// got 000,000.12
System.out.println(OUTPUT_FORMAT.format(123456));
// got 123,456.00
System.out.println(OUTPUT_FORMAT.format(123456.78));
// got 123,456.78
System.out.println(OUTPUT_FORMAT.format(1234));
// got 001,234.00
System.out.println(OUTPUT_FORMAT.format(1234.56));
// got 001,234.56
SYMBOLS.setZeroDigit('_');
OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);
System.out.println(OUTPUT_FORMAT.format(0.01));
// expected ______._1 but got ___,___._`
System.out.println(OUTPUT_FORMAT.format(0.12));
// expected ______.12 but got ___,___.`a
System.out.println(OUTPUT_FORMAT.format(123456));
// expected 123,456.__ but got `ab,cde.__
System.out.println(OUTPUT_FORMAT.format(123456.78));
// expected 123,456.78 but got `ab,cde.fg
System.out.println(OUTPUT_FORMAT.format(1234));
// expected __1,234.00 or at least __1,234.__ but got __`,abc.__
System.out.println(OUTPUT_FORMAT.format(1234.56));
// expected __1,234.56 but got __`,abc.de
}
}
好吧,如果格式正确的话,不是很接近,而是一个空数字(带有尾随下划线): ___,___。__
无论如何,关于如何获得预期行为的建议?
How to pad numbers with underscores in Java, instead of the usual zeros ?
For example I want
- 123.45 to be formated to ___123.45 and
- 12345.67 to be formated to _12345.67
- 0.12 to be formated to ______.12
I tried lots of stuff and closest I got was this (by using SYMBOLS.setZeroDigit('_');):
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class Example {
public static void main(String[] args) {
DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols();
SYMBOLS.setDecimalSeparator('.');
SYMBOLS.setGroupingSeparator(',');
DecimalFormat OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);
System.out.println(OUTPUT_FORMAT.format(0.01));
// got 000,000.01
System.out.println(OUTPUT_FORMAT.format(0.12));
// got 000,000.12
System.out.println(OUTPUT_FORMAT.format(123456));
// got 123,456.00
System.out.println(OUTPUT_FORMAT.format(123456.78));
// got 123,456.78
System.out.println(OUTPUT_FORMAT.format(1234));
// got 001,234.00
System.out.println(OUTPUT_FORMAT.format(1234.56));
// got 001,234.56
SYMBOLS.setZeroDigit('_');
OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);
System.out.println(OUTPUT_FORMAT.format(0.01));
// expected ______._1 but got ___,___._`
System.out.println(OUTPUT_FORMAT.format(0.12));
// expected ______.12 but got ___,___.`a
System.out.println(OUTPUT_FORMAT.format(123456));
// expected 123,456.__ but got `ab,cde.__
System.out.println(OUTPUT_FORMAT.format(123456.78));
// expected 123,456.78 but got `ab,cde.fg
System.out.println(OUTPUT_FORMAT.format(1234));
// expected __1,234.00 or at least __1,234.__ but got __`,abc.__
System.out.println(OUTPUT_FORMAT.format(1234.56));
// expected __1,234.56 but got __`,abc.de
}
}
Well, not really close but an empty number if formated right (with trailing underscores):
___,___.__
Anyway, suggestions on how to get the expected behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其中
num
是保存您的号码的String
。要将double
转换为String
,您可以使用Double.toString(myDouble)
。where
num
is aString
holding your number. To convert adouble
to aString
, you can useDouble.toString(myDouble)
.您可以使用 来自 Apache Commons 库的
StringUtils.leftPad()
方法。You can use the
StringUtils.leftPad()
method from the Apache Commons library.Da daaa
输出:
也许替换可以一步完成,但我会把它交给你。
Da daaa
Output:
Probably the replacement could be done in one step but I'll let that to you.