Java:从 INT 的其余部分拆分最后一位数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4135318/
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: Split last digit from the rest of the INT
提问by BoDiE2003
I have the next code, which gets a number, I need to split that number in to parts, firstPart should be the whole number without the last digit, and the secondPart should be only the last digit of the original number.
我有下一个代码,它获取一个数字,我需要将该数字分成几部分,firstPart 应该是没有最后一位数字的整数,而 secondPart 应该只是原始数字的最后一位数字。
public Boolean verificationNumberFiscalId(String fiscalId) {
// Trying to do a better wa yto do it
Integer firstPart = fiscalId.length()-1;
Integer secondPart = ??????????;
// My old logic
String NitValue = "";
for (Integer i = 0; i < fiscalId.length()-1; i++) {
NitValue = fiscalId.substring(i,1);
}
Integer actualValueLength = fiscalId.length()-1;
String digitoVerificador = fiscalId.substring(actualValueLength,1);
return this.generateDigitoVerification(NitValue) == digitoVerificador;
}
/**
* @param nit
* @return digitoVerificador
* @comment: Does the math logic
*/
public String generateDigitoVerification(String nit) {
Integer[] nums = { 3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71 };
Integer sum = 0;
String str = String.valueOf(nit);
for (Integer i = str.length() - 1, j=0; i >= 0; i--, j++) {
sum += Character.digit(str.charAt(i), 10) * nums[j];
}
Integer dv = (sum % 11) > 1 ? (11 - (sum % 11)) : (sum % 11);
return dv.toString();
}
Could you suggest me a better way to do it please? Thank you!
你能建议我一个更好的方法吗?谢谢!
回答by austinbv
int x = 1343248724;
int firstpart = x/10;
int secondpart = x%10;
System.out.println(firstpart);
System.out.println(secondpart);
Mod (%) gives you the remainder which will be the last digit if you mod 10 Divide will drop the last digit. That way you don't have to actually search the string.
Mod (%) 为您提供余数,如果您使用 mod 10 Divide 将删除最后一位数字,则该余数将成为最后一位数字。这样您就不必实际搜索字符串。
Doing all the length stuff seems a little like overkill to me
做所有长度的事情对我来说似乎有点矫枉过正
oh and to get your param as a int I would use
哦,为了让你的参数作为 int 我会使用
Interger.decode(fiscalId);
回答by Sean Patrick Floyd
This is how you can get the Integers:
这是获得整数的方法:
Integer firstPart = Integer.valueOf(
fiscalId.substring(0,fiscalId.length()-1));
Integer secondPart = Integer.valueOf(
fiscalId.substring(fiscalId.length()-1));
but I'd suggest getting ints instead:
但我建议改为使用整数:
int firstPart = Integer.parseInt(fiscalId.substring(0,fiscalId.length()-1));
int secondPart = Integer.parseInt(fiscalId.substring(fiscalId.length()-1));
回答by darioo
- Convert your number to a string. Use
String.valueOf()
- Split that string into two strings. First, look at original string's length. For length
n
, use it's substring of (0,n-1) as the first new string. Use the last character (n) for the second string - Use
Integer.valueOf()
to convert both strings back to integers.
- 将您的数字转换为字符串。利用
String.valueOf()
- 将该字符串拆分为两个字符串。首先,查看原始字符串的长度。对于 length
n
,使用它的 (0,n-1) 的子字符串作为第一个新字符串。使用最后一个字符 (n) 作为第二个字符串 - 使用
Integer.valueOf()
转换两个字符串到整数。