델파이에서 숫자 3자리마다 콤마(,)를 넣어주기 위해서는 아래와 같이

FormatFloat(',0', someInt);

를 이용하여 처리할 수 있다.

하지만, 정수형 데이터를 콤마 변환 하기 위해서 Float 함수를 사용하는 것은 불필요한 계산 시간을 유발하며, 적은 값의 데이터를 처리한다 치더라도 초당 몇천회의 불필요한 연산이 수행될 수 있습니다.

아래의 함수는 정수만을 받아들여, 콤마 변환합니다.

function IntToStrComma( Value: integer ): string;
var len, i: integer;
    buf: array [0..14] of char;
    isneg: boolean;
begin
     if Value = 0 then
     begin
          Result := '0';
          Exit;
     end;
     asm
        (* EDX:EAX = dividend, EAX = quotient
         * ECX = divisor
         * EDX = remainder
         * EBX = buf offset
         * ESI = counter for inserting commas
         *)
        PUSH EBX // save regs
        PUSH ESI

        XOR EBX, EBX
        MOV EAX, Value // get num
        // negative?
        CMP EAX, 0
        JGE @Positive
        MOV IsNeg, True
        NEG EAX
        JMP @WasNeg
        @Positive:
        MOV IsNeg, False
        @WasNeg:
        // first divide
        CDQ
        MOV ECX, 10
        IDIV ECX
        // ans now in EAX, rem in EDX
        ADD DL, 48 // convert to ascii
        MOV Byte(buf[EBX]), DL
        INC EBX
        MOV ESI, 1 // comma
        @WhileTop:  // WHILE EAX > 0 DO
            CMP EAX, 0
            JE @WhileBottom
            CDQ // these must be done again
            MOV ECX, 10
            IDIV ECX
            // ans now in EAX, rem in EDX

            // comma
            INC ESI
            CMP ESI, 4
            JNE @NoComma
            MOV byte(buf[EBX]), ','
            INC EBX
            MOV ESI, 1
            @NoComma:

            ADD DL, 48 // convert to ascii
            // save to string
            MOV byte(buf[EBX]), DL
            INC EBX
            JMP @WhileTop
        @WhileBottom: // END OF WHILE
        MOV [len], EBX
        //INC [len]
        POP ESI // restore regs
        POP EBX
     end;
     if IsNeg then
     begin
          buf[len] := '-';
          inc(len);
     end;
     (* At this point, buf contains REVERSED string; copy and reverse *)
     SetLength( Result, len );
     for i := 1 to len do
     begin
          Result[i] := Char(buf[len-i]);
     end;
end;

출처 : http://www.codexterity.com/


2008/03/04 20:34 2008/03/04 20:34
포스팅이 유익 하셨다면 RSS 구독을 신청하세요

Trackback Address >> http://dolba.net/tt/k2club/trackback/1731