James Dunay
James Dunay

Reputation: 2724

Help with formatting numbers, objective-c

I have the following,

int intMilliseconds = rawSplit % 1000;

which returns the milliseconds left from a time that i have entered, however it in very few cases returns three number (###) instead of the usual two (##).

How might i get it to always return two numbers? (##)

Any help would be great thanks !

Upvotes: 0

Views: 247

Answers (2)

jscs
jscs

Reputation: 64002

It's going to be a three-digit number any time rawSplit is a number between x,099 and x,999. The modulo operator % gives you the remainder after dividing by the right-hand operand. If you perform 1064 % 1000, the result is 64. If you perform 1548 % 1000, the result is 548.

You can change the information you're getting -- performing modulo 100, for example: 1064 % 100 -> 64 and 1548 % 100 -> 48 -- but you can't make this expression "return" two digits all the time.

From my comment below:

I guess you could use "centiseconds"; just divide your milliseconds by ten and drop the fractional part.

Upvotes: 0

Luke
Luke

Reputation: 7210

Sounds like what you want to do is truncate your milliseconds to hundredths of a second. You could do something like this:

int hundredths= (int)( (float)intMilliseconds / 10.0f ) * 10;

Here, for example, 934 or 939 milliseconds will come out as 93 hundredths of a second.

By the way, your phrasing is a little odd - there's really no way to 'format' a number of milliseconds (from 0 to 999) as a two-digit number. Because your format is xx:xx:xx, I'm assuming you mean you want to display hundredths of a second.

Upvotes: 1

Related Questions