If i'm giving values like a=1234, i want to print last two digits 34 only.. can anyone give me solution for this...
int a=1234;
System.out.print(a);
number % 100
will result in last 2 digit
See
Assuming this use-case is for visual output only:
Cast the integer to a string and get its last two characters.
int b = 1909;
String bStr = String.valueOf(b);
System.out.print(bStr.substring(bStr.length()-2));
This works only for integers with at least two digits though. So you might want to make sure you won't get a one-digit integer.
Also, feel free to use a constant instead of the magic number 2.
User modulo 100
System.out.print(String.format("%02d", (abs(a)%100)));
You can try this too
int a=1234;
System.out.print(String.valueOf(a).substring(2));
Out put
34
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With