How to format numbers properly?

slinga

Established Member
How do I format number properly? I want the output in digit form (ie 1-9) without the excess zeroes that slPrintFX gives me. How do I overcome that? For example, if I have print the number of saves on a device it will give me 0.00030 instead of 30. I know I can multiply by 1000 or so, but is there a better way? Thanks.
 

vbt

Staff member
why not using a sprintf ?

Code:
char toto[50];

sprintf("%d",int_value);

slPrint(toto,....);
!
 

ExCyber

Staff member
You could use slDispHex, though it has its own quirks. I think the SGL print routines are really only intended for debug output; developers were probably expected to write their own fancy-pants stuff if they wanted to do any kind of interesting printing.
 
The sprintf is a good solution. You have to

Code:
#include <stdio.h>

in order to use it.

random doc : ttp://www.cplusplus.com/ref/cstdio/sprintf.html
 

antime

Extra Hard Mid Boss
If you don't need to print floating-point numbers, use iprintf instead. The floating-point support in sprintf and friends mean the floating-point emulation library needs to be linked as well, which will grow your binaries considerably. (iprintf is a newlib function, not standard C.)
 
What is this floating point emulation library ? I can compile

Code:
float a = 19.7;

without linking to anything particular.

Am I missing something ?
 

antime

Extra Hard Mid Boss
It's automagically linked, so you don't have to do anything.

Also, your example will not link the library, it'll just put the floating-point constant "19.7" into the object file. The difference is when you actually perform operations using floating-point numbers.

This compiles into an 11093 byte object file:
Code:
int main(void)

{

    float a = 19.4;

    float b = 5.3;

    return 0;

}

This, on the other hand, becomes a 29830 byte file:
Code:
int main(void)

{

    float a = 19.4;

    float b = 5.3;

    float c = a + b;

    return 0;

}

Looking at the produced code with objdump we see that the difference in size is made up by the inclusion of soft-float routines. (IIRC, the soft-float code along with lots of other support code is located in libgcc.)
 
Yes, but it is hard not to use floats, as it is even used in sl macros :

from sl_def.h

Code:
#define   toFIXED(a) ((FIXED)(65536.0 * (a)))
 

slinga

Established Member
Thanks for your responses guys.

sldisphex: displays hex argh.

sprintf: works, but the text seems to be in slightly different positions. Oh well I'll fomart it again.

iprintf: I couldn't ge this to work.
 

antime

Extra Hard Mid Boss
Originally posted by vreuzon@Aug 29, 2003 @ 03:22 PM

Yes, but it is hard not to use floats, as it is even used in sl macros

As long as those macros are used only in constant expressions, they should be optimized away at compile-time.
 
Top