News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Formating Signed string

Started by Farabi, July 27, 2010, 11:19:19 AM

Previous topic - Next topic

Farabi

Hi,
On my currency if you want to write 1 million is 1.000.000 but on English it was 1,000,000 is there any function to format the string to my currency format?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Geryon

"Some people have got a mental horizon of radius zero and call it their point of view." --D.Hilbert

frktons

Mind is like a parachute. You know what to do in order to use it :-)

frktons

And if you speak C, this is the C version that does the same for
integer number only:

//-----------------------------------------------------------------------------------------------
// Function prototype for itoas(). Takes in the number to
// format, the pointer of the buffer to fill, and the thousand separator
// to use. Returns the lenght of the generated string.
//-----------------------------------------------------------------------------------------------

int itoas(long num, char [], char sep);


and:


//---------------------------------------------------------------
// itoas() - iMalc version updated ver. 0.8
//---------------------------------------------------------------
int itoas(long snum, char s[], char sep)
{
    char *ps = s;
    unsigned long num1 = snum, num2, num3, div;
    if (snum < 0) {
        *ps++ = '-';
        num1 = -snum;
    }
    if (num1 < 10000) {
        if (num1 < 10) goto L1;
        if (num1 < 100) goto L2;
        if (num1 < 1000) goto L3;
    } else {
        num2 = num1 / 10000;
        num1 -= num2 * 10000;
        if (num2 < 10000) {
            if (num2 < 10) goto L5;
            if (num2 < 100) goto L6;
            if (num2 < 1000) goto L7;
        } else {
            num3 = num2 / 10000;
            num2 -= num3 * 10000;
            if (num3 >= 10) {
                *ps++ = '0' + (char)(div = (num3*6554UL)>>16); num3 -= div*10;
                *ps++ = sep;
            }
            *ps++ = '0' + (char)(num3);
        }
        *ps++ = '0' + (char)(div = (num2*8389UL)>>23); num2 -= div*1000;
L7:
        *ps++ = '0' + (char)(div = (num2*5243UL)>>19); num2 -= div*100;
        *ps++ = sep;
L6:
        *ps++ = '0' + (char)(div = (num2*6554UL)>>16); num2 -= div*10;
L5:
        *ps++ = '0' + (char)(num2);
    }
    *ps++ = '0' + (char)(div = (num1*8389UL)>>23); num1 -= div*1000;
    *ps++ = sep;
L3:
    *ps++ = '0' + (char)(div = (num1*5243UL)>>19); num1 -= div*100;
L2:
    *ps++ = '0' + (char)(div = (num1*6554UL)>>16); num1 -= div*10;
L1:
    *ps++ = '0' + (char)(num1);
    *ps = '\0';
    return ps - s;
}


to adapt it to your needs, or to translate it into ASM is up to you.
I didn't translate it yet.

Enjoy
Mind is like a parachute. You know what to do in order to use it :-)

Farabi

Quote from: frktons on July 27, 2010, 12:48:54 PM
And if you speak C, this is the C version that does the same for
integer number only:

//-----------------------------------------------------------------------------------------------
// Function prototype for itoas(). Takes in the number to
// format, the pointer of the buffer to fill, and the thousand separator
// to use. Returns the lenght of the generated string.
//-----------------------------------------------------------------------------------------------

int itoas(long num, char [], char sep);


and:


//---------------------------------------------------------------
// itoas() - iMalc version updated ver. 0.8
//---------------------------------------------------------------
int itoas(long snum, char s[], char sep)
{
    char *ps = s;
    unsigned long num1 = snum, num2, num3, div;
    if (snum < 0) {
        *ps++ = '-';
        num1 = -snum;
    }
    if (num1 < 10000) {
        if (num1 < 10) goto L1;
        if (num1 < 100) goto L2;
        if (num1 < 1000) goto L3;
    } else {
        num2 = num1 / 10000;
        num1 -= num2 * 10000;
        if (num2 < 10000) {
            if (num2 < 10) goto L5;
            if (num2 < 100) goto L6;
            if (num2 < 1000) goto L7;
        } else {
            num3 = num2 / 10000;
            num2 -= num3 * 10000;
            if (num3 >= 10) {
                *ps++ = '0' + (char)(div = (num3*6554UL)>>16); num3 -= div*10;
                *ps++ = sep;
            }
            *ps++ = '0' + (char)(num3);
        }
        *ps++ = '0' + (char)(div = (num2*8389UL)>>23); num2 -= div*1000;
L7:
        *ps++ = '0' + (char)(div = (num2*5243UL)>>19); num2 -= div*100;
        *ps++ = sep;
L6:
        *ps++ = '0' + (char)(div = (num2*6554UL)>>16); num2 -= div*10;
L5:
        *ps++ = '0' + (char)(num2);
    }
    *ps++ = '0' + (char)(div = (num1*8389UL)>>23); num1 -= div*1000;
    *ps++ = sep;
L3:
    *ps++ = '0' + (char)(div = (num1*5243UL)>>19); num1 -= div*100;
L2:
    *ps++ = '0' + (char)(div = (num1*6554UL)>>16); num1 -= div*10;
L1:
    *ps++ = '0' + (char)(num1);
    *ps = '\0';
    return ps - s;
}


to adapt it to your needs, or to translate it into ASM is up to you.
I didn't translate it yet.

Enjoy

I prefer Ghandi method. Easier and simpler.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

frktons

Quote from: Farabi on July 27, 2010, 01:47:33 PM
I prefer Ghandi method. Easier and simpler.

The bazaar is open till midnight, choose whatever you like  :P

By the way, the C version is the fastest, in case you need this info.  :U
Mind is like a parachute. You know what to do in order to use it :-)

hoverlees

But I want to know which one is the float point of your currency?  :eek

hutch--

Onan,

Just get the length of the currency string, see how many 3 character blocks are in it them write the string to another larger buffer with the added characters for your currency format.

here is another way to do it as long as there is no fraction and decimal point. IE "1234567890" but not "1234567890.00".


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    fmtstr PROTO :DWORD,:DWORD

    .data
      curr db "1234567890",0
      buff db "                              ",0

      pcur dd curr
      pbuf dd buff

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    invoke fmtstr,pcur,pbuf

    print pbuf,13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

fmtstr proc psrc:DWORD,pdst:DWORD

    push ebx

    invoke szRev,psrc,psrc      ; overwrite original

    mov ecx, psrc
    mov edx, pdst
    xor ebx, ebx

  lbl0:
    mov al, [ecx]
    test al, al
    jz lbl2
    add ecx, 1
   
    cmp ebx, 3
    jne lbl1

  ; ---------------------------------------
  ; add the seperator every third character
  ; ---------------------------------------
    mov BYTE PTR [edx], ","     ; put your preferred character here.
    add edx, 1
    xor ebx, ebx                ; reset counter to zero
  ; ---------------------------------------

  lbl1:
    add ebx, 1
    mov [edx], al
    add edx, 1
    jmp lbl0

  lbl2:
    mov BYTE PTR [edx], 0       ; terminate output buffer
    invoke szRev,pdst,pdst      ; reverse the output buffer

    pop ebx

    ret

fmtstr endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Farabi

Quote from: hutch-- on July 28, 2010, 05:36:38 AM
Onan,

Just get the length of the currency string, see how many 3 character blocks are in it them write the string to another larger buffer with the added characters for your currency format.

Yeah I did. Thanks anyway.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

raymond

Quote from: hoverlees on July 28, 2010, 05:31:19 AM
But I want to know which one is the float point of your currency? :eek

Many countries use the coma as the decimal delimiter (instead of the "decimal point").
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

hoverlees

Quote from: raymond on July 29, 2010, 01:24:50 AM
Quote from: hoverlees on July 28, 2010, 05:31:19 AM
But I want to know which one is the float point of your currency? :eek

Many countries use the coma as the decimal delimiter (instead of the "decimal point").

thanks :bdg

Geryon

As you notice my english is not so good
No offence, but Isn't it that you are trying to achieve ?
"Some people have got a mental horizon of radius zero and call it their point of view." --D.Hilbert

frktons

Quote from: Geryon on July 29, 2010, 09:22:43 AM
As you notice my english is not so good
No offence, but Isn't it that you are trying to achieve ?

Yes sir. That's exactly what farabi was looking for. Maybe he doesn't like
calling APIs functions, so he preferred to use an hand-made solution.  :P

Any reason you used test.bat instead of test.asm?

And a last thing, in order to compile with qeditor and MASM32 package,
I had to modidy some small things:


;---------------------------------------------------------------------------------
; Format currency values with thousand separator, decimal point and
; currency symbol based on LOCALE_USER_DEFAULT.
;---------------------------------------------------------------------------------

.nolist
include \masm32\include\masm32rt.inc

.686

.data
    szMoney     DB  "12345678910", 0
    szTitle     DB  "Currency Test", 0
   
.data?
    szBuffer    DB  255 dup(?)

.code
start:
    invoke GetCurrencyFormat, LOCALE_USER_DEFAULT  \
                            , 0                    \
                            , offset szMoney       \
                            , NULL                 \
                            , offset szBuffer      \
                            , 255
                           
    invoke MessageBox, NULL                     \
                     , offset szBuffer          \
                     , offset szTitle           \
                     , MB_OK or MB_SYSTEMMODAL 
                     
    invoke ExitProcess, -1
end start


It was ready for EasyCode, probably not for qeditor  ::)

Mind is like a parachute. You know what to do in order to use it :-)

jj2007

Just a small hint: With invoke, you don't need backslashes, a comma is enough

    invoke GetCurrencyFormat, LOCALE_USER_DEFAULT,
    0,
    offset szMoney,
    NULL,
    offset szBuffer,
    SIZEOF szBuffer


Or, on one line: invoke GetCurrencyFormat, LOCALE_USER_DEFAULT, 0, offset szMoney, NULL, offset szBuffer, SIZEOF szBuffer

Geryon

Quote from: frktons on July 29, 2010, 11:57:27 AM
Any reason you used test.bat instead of test.asm?
Yes, when I double click that .bat file, it will begin to execute masm compile commands.
So, without a IDE, it provide easy build.

Quote from: jj2007 on July 29, 2010, 12:41:03 PM
Just a small hint: With invoke, you don't need backslashes, a comma is enough
Thank you, I will do that.
Acutally, I've been always confused about that.
Let's take realy long line

invoke Function, arg1, arg2, arg3, arrrrrrrrrg4, arg5, ..... etc

How should I divide it ?
like that (C style)

invoke function arg1,
arg2,
arg3,
etc...


or like this

invoke function arg1, arg2, arg3
arg4, arg5, arg6
"Some people have got a mental horizon of radius zero and call it their point of view." --D.Hilbert