需要 parseInt 方面的帮助
例如,我如何从字符串 code='A082' 获取整数 codeInt=082 我已经尝试过这个:
int codeInt = Integer.parseInt(code.substring(1,4));
我得到 codeInt=82 ,它留下了第一个 0 但我想要完整的代码“082”。
我想到了 parseInt(String s, int radix)
但我不知道如何。
任何帮助将不胜感激。
谢谢。
how can i get for example the integer codeInt=082 from String code='A082'
i have tried this:
int codeInt = Integer.parseInt(code.substring(1,4));
and i get codeInt=82 ,it leaves the first 0 but i want the full code '082'.
i thought of parseInt(String s, int radix)
but i don't know how .
any help will be appreciated .
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
整数仅存储一个数字。数字 82 和 082(以及 0082 和 000000082)完全相同(除非您以这种方式将它们放入某些语言的源代码中,否则您将收到编译器错误)1 。
如果您迫切需要前导零,那么您应该将其保留为字符串,或者适当地格式化数字以便稍后输出。
1 由于 C 设计者有一个巧妙的想法,即在前面加上零来编写八进制常量会很酷。好像一旦您已经获得了
0xf00
之类的东西,0o123
就很难实现了......An integer just stores a number. The numbers 82 and 082 (and 0082 and 000000082 for that matter) are exactly the same (unless you put them into source code in some languages in that manner, then you'll get a compiler error)1.
If you desperately need the leading zero, then you should either leave it as a string, or format the number appropriately for output later.
1 Due to the C designers having the ingenious idea that writing octal constants with a preceding zero would be cool. As if something like
0o123
would have been that hard to implement once you already got0xf00
...数字
82
和082
和0082
在数学上是相同的数字,并且由相同的位序列表示。您无法在int
中对前导零的数量进行编码(尽管您当然可以使用您选择的任何格式打印它)。另请注意,数字
082
与Java 文字082
不同,后者是一个(无效的)八进制文字。The number
82
and082
and0082
is mathematically the same number, and is represented by the same sequence of bits. You can't encode the number of leading zeroes in anint
(although you can certainly print it with whatever format you choose).Note also that the number
082
is different from the Java literal082
, which is an (invalid) octal literal.082 不是整数。它是一个表示整数 82 的字符串。如果您需要保持前导零不变,则需要使用字符串。如果您只需要打印 082,则可以使用 java.text.MessageFormat 或 System.out.format() 或其他类似的解决方案来以这种方式打印它。
082 is not an integer. It's a string representing the integer 82. If you require leading zeros to be left untouched, you will need to work with strings. If you only need it to print 082, you can use
java.text.MessageFormat
orSystem.out.format()
or other, similar solutions to print it that way.如果您想要
0000123
,那么您需要将变量威胁为字符串而不是整数。简单地说:123
等于000123
和0123
以及0000...这里有 10 亿个零...000123
。但如果您只想显示固定长度的数字,请使用 System.out.format()。
If you want
0000123
then you need to threat a variable as a String instead of Integer. Simply:123
is equal to000123
and0123
and0000...1 billion zeros here...000123
.But if you just want to display a number with fixed length then use
System.out.format()
.