Java 为数字添加前导零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2555914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java add leading zeros to a number
提问by user69514
I need to return a string in the form xxx-xxxx where xxx is a number and xxxx is another number, however when i have leading zeros they disappear. I'm trying number formatter, but it's not working.
我需要以 xxx-xxxx 形式返回一个字符串,其中 xxx 是一个数字,而 xxxx 是另一个数字,但是当我有前导零时,它们就会消失。我正在尝试数字格式化程序,但它不起作用。
public String toString(){
NumberFormat nf3 = new DecimalFormat("#000");
NumberFormat nf4 = new DecimalFormat("#0000");
if( areaCode != 0)
return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
else
return exchangeCode + "-" + number;
}
}
}
I figured it out:
我想到了:
public String toString(){
NumberFormat nf3 = new DecimalFormat("000");
NumberFormat nf4 = new DecimalFormat("0000");
if( areaCode != 0)
//myFormat.format(new Integer(someValue));
return nf3.format(new Integer(areaCode)) + "-" + nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
else
return nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
}
采纳答案by Justin Ardini
When areaCode is 0, you forget to call format
! Other than that, it looks fine. The leading "#" are not necessary, but won't cause any problems for valid inputs.
当 areaCode 为 0 时,您忘记调用format
! 除此之外,它看起来不错。前导“#”不是必需的,但不会对有效输入造成任何问题。
I just tried it out real quick to check and it worked fine for me.
我刚刚试了一下,很快就检查出来了,对我来说效果很好。
public static String formatTest(int areaCode, int exchangeCode, int number) {
DecimalFormat nf3 = new DecimalFormat("#000");
DecimalFormat nf4 = new DecimalFormat("#0000");
if( areaCode != 0)
return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
else
return nf3.format(exchangeCode) + "-" + nf4.format(number);
}
public static void main(String[] args) {
System.out.println(formatTest(12, 90, 8));
System.out.println(formatTest(1, 953, 1932));
}
Output:
输出:
012-090-0008
001-953-1932
回答by Holograham
Remove the # sign
去掉#号
http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
This code:
这段代码:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Test
{
public static void main(String[] args)
{
int areaCode = 123;
int exchangeCode = 456;
NumberFormat nf3 = new DecimalFormat("0000");
System.out.println(nf3.format(areaCode) + "-" + nf3.format(exchangeCode) );
}
}
Produces this output:
产生这个输出:
0123-0456
0123-0456
回答by Tomislav Nakic-Alfirevic
There's an arguably more elegant solution:
有一个可以说是更优雅的解决方案:
String.format("%03d-%03d-%04d", areaCode, exchangeCode, number)
回答by Kuchi
I would recommend using the NumberFormat (http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html)
我建议使用 NumberFormat ( http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html)
In my opinion it gives the best readability. And also minimizes the possibility of errors when you put a wrong String into the DecimalFormat.
在我看来,它提供了最好的可读性。当您将错误的字符串放入 DecimalFormat 时,还可以最大限度地减少出错的可能性。
final int value1 = 1;
final double value2 = 4.2;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMinimumIntegerDigits(2);
System.out.println(nf.format(value1));
System.out.println(nf.format(value2));
Output:
输出:
01
04.2
(The Locale is optional but I recommend it when you work with an international team. Default value are the local settings)
(Locale 是可选的,但当您与国际团队合作时,我建议您使用它。默认值是本地设置)
Anyway NumberFormat in this way is such a great thing, especially if you have to deal with different countries or things like percentage or currency
无论如何,以这种方式 NumberFormat 是一件很棒的事情,特别是如果您必须处理不同的国家或地区,例如百分比或货币
回答by imansdn
you can use NumberFormat class and set the minimum zeros as 0 easliy and it works like a charm for example for currency format:
您可以使用 NumberFormat 类并将最小零设置为 0 easliy,它就像一个魅力,例如货币格式:
formatter = NumberFormat.getCurrencyInstance();
formatter.setMinimumFractionDigits(0);
-----------
print ( formatter.format(119.00) ) ---> 9