This is not anything fantastic, but i thought i would share it for those who care.

I needed to clean up floats for edit box outputs, but discovered wsprintf was useless. So i wrote a quick routine to format my floats to 3 decmal places.

I did not formating before the decmal, just after.

Float2FmtString   PROTO       :DWORD, :PTR REAL8, :PTR DWORD

Float2FmtString PROC uses esi edi ebx decmals:DWORD, lpb:PTR REAL8, lpout:PTR DWORD
LOCAL mbuf[260] :BYTE

mov esi, lpb
mov ecx, decmals
xor edx, edx
inc edx
.while (ecx)
imul edx, 10
dec ecx
mov ina, edx ;; ina = 10^decmal

fld REAL8 PTR [esi]
fild ina
fistp outa ;; outa = (int)(float * ina)

invoke dwtoa, outa, lpout ;; lpout == converted string( outa )
invoke StrLen, lpout ;; Get size

mov edx, ina
.if( outa >= edx) ;; if Number >= 1

mov esi, lpout
add esi, eax
mov edi, decmals
inc edi
xor eax, eax
mov al, [esi]
mov [esi + 1], al
dec esi
dec edi
jnz @B
inc esi
mov eax, 2Eh
mov [esi], al

.else ;; number is < 1

mov edi, decmals
sub edi, eax ;; See how many places to fill

xor eax, eax
lea esi, mbuf
mov [esi], eax
invoke szCatStr, esi, lpout ;; Copy the converted data

mov esi, lpout
xor eax, eax
mov DWORD PTR [esi], 2E30h ;; Prefix with "0.",null
inc esi
inc esi
.while (edi)
mov DWORD PTR [esi], 30h ;; Add needed zeros to string with null's
inc esi
dec edi
invoke szCatStr, lpout, addr mbuf ;; copy to output converted string


Float2FmtString ENDP

Use it like so (in this case three decmal places):

invoke Float2FmtString, 3, addr MyReal8, addr szBuffer

If the number was 123.45678, it would return "123.457",NULL

Posted on 2003-01-09 21:38:50 by NaN
NaN,nice algo!:alright:


Posted on 2003-01-10 01:27:19 by Vortex
Thanx im glad you like it.

Its Real Value range depends on the decmal place. Basically the equation is : (2 BILLION / (10^Decmals) ). As you can see.. Large demal places need small Real componenets to work. This is because of the limitations of DWORDS. Pehaps i will make something more robust. But there will be alot more code involved and would require bit analysis of the float with IEEE standard. (something i dont know off the top of my head ;) )

This above version uses the FPU to do most of this dirty work for me ;) . But it worked well for my application:
Posted on 2003-01-11 15:34:17 by NaN
Not to discourage you NaN, but the printf routine in C libraries might be useful. I haven't even used it myself except in Privalovs clib Linux example. I think it was Vortex who showed how to access the c library dll from windows.
Posted on 2003-01-12 06:45:25 by Eóin
Thanx E?in for the suggestion, but im a bit of a purest with MASM. As well, im not sure if i would even have this standard lib on my CPU. I have VC++ installed, so probably. But to be honest with you im illiterate when it comes to programming with it ;) . I can read C++ sources fine, just dont ask me to create them (hurts my head thinking about it now ;) )

Another big factor is that MASM is licience free. Using C routines may violate this perk...
Posted on 2003-01-12 09:30:00 by NaN
I'm inclined to agree with NaN, it just seemed like an easy way out. Plus I like to way it make formatting strings easy like wsprintf.
Posted on 2003-01-12 10:40:30 by Eóin

Another big factor is that MASM is licience free. Using C routines may violate this perk...

Dear NaN,

Crtdll.dll is used by the Lcc-Win32 compiler. (Lcc is free to use)
I don't think that using this dll violates the rules.

Posted on 2003-01-12 12:11:30 by Vortex
Vortex. Nice work!

For others who dont frequent the FASM dir (like me ~ no offense to anyone).

Here is the link discussing the topic:

I read the post over, and did a quick check on my system. Both the msvcrt.dll and crtdll.dll is found in my WINDOWS\SYSTEM directory. My OS is win98SE.

My question is it in everyone else's system???
Could readers of this thread search and validate this assumption? (NT,95,Win2000,XP,ME,98).

Posted on 2003-01-13 00:14:24 by NaN
NaN, Im running Windows/2K and both files are in my system. Looking
at the file time it seems they were installed with the system. Since both files
belong to the microsoft c runtime library. I think they are always included in a Windows OS.(or alteast in W/2K)(microsoft vb runtimes are also included in
the OS as standard) NOTE: I dont have C/C++ Installed on my system.

Oh, BTW: your routine looks pretty damn good. :alright:
Posted on 2003-01-13 00:27:17 by natas
Thanks Nan.The C run-time dlls are also included in Win95b and WinXP.


Posted on 2003-01-13 02:02:33 by Vortex