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.
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
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.
精彩评论