Skip to content

Improve code for time digit handling #10

@cbonsig

Description

@cbonsig

From Robert on the clock.bonsignore.com Tumblr site:

I have some ideas on how to clean up your code and maybe add functionality. But I don't know how to contact you, e-mail or what.
For starters, here are some tricks for dealing with numbers:
If n is a number ("byte" or "int"), then as long as it is less than 100:
(n / 10) is the tens' digit;
(n % 10) is the ones' digit.
Mathematically, it works like this: Suppose, for example, that n is 26. Dividing 26 by 10 gives you a quotient of 2 and a remainder of 6. The / (slash) gives you the quotient and the % gives you the remainder. That's how it works.
Another trick you can use is this: if h is hours in 24-hour format, then (h % 12) gives you the remainder from dividing by 12. Of course, it will go from 0 to 11, rather than 1 to 12, which is why it will need to be adjusted. But even for that, there is a trick: ((h%12) == 0 ? 12 : (h%12)) will check for the special case and change 0 to 12.

Like I told you (and I will now expand on it):
If "n" is an integer, then as long as "n" is non-negative:
(n % 10) is our ones digit;
((n / 10) % 10) is our tens digit;
((n / 100) % 10) is our hundreds digit;
((n / 1000) % 10) is our thousands digit;
((n / 10000) % 10) is our ten thousands digit;
and so forth, up to the limit of our datatype.
This way, we do not have to mess around with string manipulation.
And leading zeros (such as for minutes) are provided automatically.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions