bratao
bratao

Reputation: 2070

How to get the n-last digits of a number?

I´m converting a Linux Logger to work in windows. The logger prints with snprintf. In linux, this logger output timeofday.tv_usec , that give something like this :

Jun 24 18:30:31-232928 test-transport...

In my windows version, using QueryPerformanceCounter, i generate results like this:

jun 24 23:54:18-866568508 test-transport....

In Linux, the uSeconds have exactly 6 digits, but this windows function generate 9 digits. How could i print only the 6 last digits ? Remember that this is a time critical code.

Upvotes: 2

Views: 3122

Answers (2)

Alok Singhal
Alok Singhal

Reputation: 96121

It really depends what the number 866568508 represents. Is this nanoseconds? Or microseconds? Or is this in the units of some other number, such as QueryPerformanceFrequency? So let's say 866568508 represents 866568508 ticks of an n Hertz clock. Then, the amount of time in seconds represented by 866568508 is 866568508/n. This is 866568508*1e6/n microseconds.

So, your idea of getting microseconds by using the last 6 digits is not necessarily correct. Since you say you usually have 9 digits, n could be 1e9 (i.e., you have nanosecond resolution). In this case, you can get microseconds from 866568508 by doing 866568508*1e6/1e9 =866568508/1e3`.

But, as I said, all this depends upon you knowing what the resolution is.

From some quick google search, it seems like QueryPerformanceFrequency should give you the frequency.

Upvotes: 0

icktoofay
icktoofay

Reputation: 129001

Use the remainder of dividing the number by 10 to the power of the number of digits you want to preserve.

In your case:

num % 1000000

Upvotes: 8

Related Questions