C/Objective-C read and get last digit of integer?
Asked Answered
E

1

17

How can i get the last digit of an integer (or NSInteger) outputted to integer?

example:

int time = CFAbsoluteGetCurrent();
int lastDigit;
Edgar answered 30/12, 2010 at 2:50 Comment(1)
Don't you just need to mod by 10 (time % 10)?Encincture
D
41

Use modulo:

int lastDigit = time % 10;
Deface answered 30/12, 2010 at 2:55 Comment(2)
wow that worked!! i wasn't thinking in simple math terms. thanks!!Edgar
This is what I used many times when I started learning programming! Simple but yet helpful. Thanks =)Reheat

© 2022 - 2024 — McMap. All rights reserved.