자리수 맞게 숫자출력???

zxcvbnm의 이미지

소스분석을 하고 있는데요. 궁금한게 있어서요.

20030909212523733

위에것은 아래 코드의 결과 값입니다. 17자리죠... 이것을 제일 마지막 숫자한자리를 제거하고 2003090921252373 이렇게 16자리로 출력하고 싶은데 잘 안되네요.
도와주세요.

struct tm *dateTime;
struct timeb tpd;
time_t clock;

ftime( &tpd );
clock = tpd.time;
dateTime = localtime(&clock);

printf("\n\n%4d%02d%02d%02d%02d%02d%2d\n\n", dateTime->tm_year+1900, dateTime->tm_mon+1, dateTime->tm_mday,dateTime->tm_hour, dat
eTime->tm_min, dateTime->tm_sec, tpd.millitm);

moonzoo의 이미지

tpd.millitm 가 3자리로 출력이 되어서 그렇습니다.

2자리로 출력할려면 10으로 나눠서 작은 자리수를 버려야 겠네요..

tpd.millitm/10 하세요.

fliers의 이미지

moonzoo wrote:
tpd.millitm 가 3자리로 출력이 되어서 그렇습니다.

2자리로 출력할려면 10으로 나눠서 작은 자리수를 버려야 겠네요..

tpd.millitm/10 하세요.

제가 그렇게 답했다가, 수업이 시작해서 지웠는데,
3자리니깐 10으로 나누면 안됩니다.. :)

tpd.millitm > 99 ? tpd.millitm / 10 : tpd.millitm

마지막에 %02d 로 포메팅하고요..

cdpark의 이미지

fliers wrote:

tpd.millitm > 99 ? tpd.millitm / 10 : tpd.millitm

마지막에 %02d 로 포메팅하고요..

위 코드는 꽤 이상하군요.

마지막 자리수가 30 이라면 30을 의미할 수도 있지만 300-309를 의미할 수도 있습니다.

그냥 10으로 나누고 %02d로 찍으면 됩니다. 이 경우 03은 30-39, 30은 300-309를 의미합니다.

fliers의 이미지

cdpark wrote:
마지막 자리수가 30 이라면 30을 의미할 수도 있지만 300-309를 의미할 수도 있습니다.

그냥 10으로 나누고 %02d로 찍으면 됩니다. 이 경우 03은 30-39, 30은 300-309를 의미합니다.

에구, 잘 못 생각했던 것 같군요 -.-;;

익명사용자의 이미지

/* 서버의 날자와 시간을 가지고온다. 시작 */

time_t Time;
struct tm *Tm;
char Date[9];
char recTime[7];

time( &Time);
Tm = localtime( &Time);
/*sprintf( Date, "%04d%02d%02d%02d%02d%02d", Tm->tm_year + 1900, Tm->tm_mon + 1, Tm->tm_mday, Tm->tm_hour, Tm->tm_min, Tm->tm_sec);*/

sprintf( Date, "%04d%02d%02d", Tm->tm_year + 1900, Tm->tm_mon + 1, Tm->tm_mday);

sprintf( recTime, "%02d%02d%02d", Tm->tm_hour, Tm->tm_min, Tm->tm_sec);

printf("\n");
printf("현재 서버날자 : [%s]\n",Date);
printf("현재 서버시간 : [%s]\n",recTime);

익명사용자의 이미지

sprintf(str, "%4d%02d%02d%02d%02d%02d%2d", dateTime->tm_year+1900, dateTime->tm_mon+1, dateTime->tm_mday,dateTime->tm_hour, dateTime->tm_min, dateTime->tm_sec, tpd.millitm);

str[strlen(str) - 1] = null;
printf("%s", str);

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.