java 是否可以将 0001 存储在 int 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17399303/
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
Is it possible to store 0001 in int?
提问by Gokul Nath KP
The product (25 years old product) I am working on is very large and complicated. So let me explain what I want in simple scenario:
我正在开发的产品(25 年的产品)非常庞大且复杂。所以让我在简单的场景中解释我想要什么:
I've declared an int
variable. Say, int var = 0001;
我已经声明了一个int
变量。说,int var = 0001;
When we try to retrieve the value of var
, we'll get 1
.
当我们尝试检索 的值时var
,我们会得到1
。
Now, my requirement is, is it possible to return 0001
, without changing the signature or return type of the method?
现在,我的要求是,是否可以在0001
不更改方法的签名或返回类型的情况下返回?
回答by Maarty
if you know how much digits number have you can use
如果你知道你可以使用多少位数
String.format("%04d", number);
int will allways return 1
int 总是返回 1
回答by Boris the Spider
You cannot store 0001
as an int
.
您不能存储0001
为int
.
An int
stores a number, i.e. 1
. You need a format.
Anint
存储一个数字,即1
。你需要一个格式。
When you "retrieve" your int
you implicitly Integer.toString()
it; this is the point at which you need to tell Java how to format it.
当你“检索”你的时候,int
你就隐含了Integer.toString()
它;这是您需要告诉 Java 如何格式化它的地方。
You haven't really given any information but I suppose you always need 4 digits, in this case the format mask 0000
ought to do the trick.
您还没有真正提供任何信息,但我想您总是需要 4 位数字,在这种情况下,格式掩码0000
应该可以解决问题。
public static void main(String[] args) throws Exception {
final int i = 1;
final DecimalFormat decimalFormat = new DecimalFormat("0000");
System.out.println(decimalFormat.format(i));
}
Output:
输出:
0001
回答by Dmitry Bychenko
Do not start numbers from 0 - in Java they considered to be octal (base 8)- unless you have strange outcomes, e.g.:
不要从 0 开始数字 - 在 Java 中他们认为是八进制(以 8为底)- 除非你有奇怪的结果,例如:
001 == 1
010 == 8 // Octal 10 == Decimal 8
0123 == 83 // Octal 123 is Decimal 83
08 - error! This's not an octal value
001 == 1
010 == 8 // 八进制 10 == 十进制 8
0123 == 83 // 八进制 123 是十进制 83
08 - error! This's not an octal value
etc.
等等。
Instead use formattingwhenever you want to print out your number
每当您想打印您的号码时,请改用格式
int number = 1; // <- Just a decimal integer
String result = String.form("My number is %04d in dddd format", number);
回答by Esko
Two possibilities:
两种可能:
- The value is actually octal and you need to take care of conversions etc. or
- This is just a data presentation problem and you need to transform it into a string with required left padding before interacting with the system that's almost as old as I am.
- 该值实际上是八进制,您需要处理转换等或
- 这只是一个数据呈现问题,在与几乎和我一样古老的系统进行交互之前,您需要将其转换为具有所需左填充的字符串。
回答by Masudul
Before zero of an int treat as Octal in Java. So, You cannot store 0001 as an int.
在 Java 中 int 的零之前视为八进制。因此,您不能将 0001 存储为 int。
回答by Berk Aydin
If you know the exact length of the number. For example 6.
如果您知道数字的确切长度。例如 6。
int x = 5
String s = "";
s = ""+x;
while(s.length() < 6){
s = "0"+s;
}
This will print 000005.
这将打印 000005。
回答by ALLU AJAY KUMAR
After some lookout i found an working answer.
经过一番观察,我找到了一个有效的答案。
DecimalFormat df=new DecimalFormat("0000");
int i=1;
i=df.format(i);
this worked for me.
这对我有用。