Reputation: 283
I have an integer with only two digits, let's say n = 52, i want to be able to separate these two digits, like 5 and 2.
int left = (n / 10);
This gives me left = 5 for n = 52.
int right = (int)(((n / 10f) - (n / 10)) * 10)
The left digit is always true, but the right digits are sometimes right and sometimes wrong, and here are the test cases:
1. 29, 48 , 10 , 50 : Correct
2. 52 : Wrong, gives 5 , 1
3. 99 : Wrong, gives 9 , 8
4. 26 : Wrong, gives 2 , 5
Upvotes: 2
Views: 556
Reputation: 886
int n = 52 ;
Solution 1 :
int left =int.Parse( n.toString().Substring(0,1)) ;
int right =int.Parse( n.toString().Substring(1,1)) ;
Solution 2 :
int left = n / 10 ;
int right = n % 10 ;
Upvotes: 3