Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Convert a float to a string?

Author
Message
Scarface
18
Years of Service
User Offline
Joined: 21st Oct 2006
Location:
Posted: 22nd Aug 2007 21:25 Edited at: 22nd Aug 2007 21:46
How would I convert a float to a string so that it may be displayed as text for example:



In another C based language I use we would do it like this:



I tried a few variations but I can't figure it out, help please

[Edit]

Ok I found the function I need, dbStr(x) however I have no idea how to link it to text, for example "x = " + dbStr(x) does not work :/

- Scarface
Red Ocktober
20
Years of Service
User Offline
Joined: 6th Dec 2003
Location:
Posted: 22nd Aug 2007 22:03 Edited at: 22nd Aug 2007 22:33
sprintf may be what you're looking for...
it's a simple way to do these conversions... from the old stdio lib...

something like this should work...


in short, you use sprintf to place the formatted string into myText... then print it as normal...

search the net for printf for more details...

good luck...


--Mike
Scarface
18
Years of Service
User Offline
Joined: 21st Oct 2006
Location:
Posted: 22nd Aug 2007 22:31
Doesn't compile, error C2065: 'f' : undeclared identifier on the sprintf line.

- Scarface
Red Ocktober
20
Years of Service
User Offline
Joined: 6th Dec 2003
Location:
Posted: 22nd Aug 2007 22:33 Edited at: 22nd Aug 2007 22:34
ooops reread the edit i made...

%f should be in quotes... (like this "%f")... SORRRRYYYYY

--Mike
Scarface
18
Years of Service
User Offline
Joined: 21st Oct 2006
Location:
Posted: 22nd Aug 2007 22:42
Ok this is what I have:



I changed it a bit to my own coding style, but it should work but all that is displayed is "f", I also wanted it to say "xPos = float" (float being the x vector).

Thanks for the help btw.

- Scarface
Karsten
17
Years of Service
User Offline
Joined: 24th Jan 2007
Location: N/A
Posted: 22nd Aug 2007 22:51 Edited at: 22nd Aug 2007 22:52
in DGDK .NET it is simply...

txtVariable = Convert.ToString(fltVariable);

Please note that i did not have intellitype when I posted this so it may be wrong haha.
Red Ocktober
20
Years of Service
User Offline
Joined: 6th Dec 2003
Location:
Posted: 22nd Aug 2007 22:56 Edited at: 22nd Aug 2007 23:02
@ Karsten
in my c++ utils lib i have a similar function... so i could call

txtVar=FtoS(fltVar);
(i think that's what i called it )


@ Scarface
look at where the % is... it's supposed to be %f... not f%...
easy enough to overlook

the % is called the lead in character... and as such, is supposed to lead the control character...
the string that controls the output is called, appropriately enough, the control string...

--Mike
Scarface
18
Years of Service
User Offline
Joined: 21st Oct 2006
Location:
Posted: 22nd Aug 2007 23:02
Karsten, I am using GDK not GDK.net, is there a GDK version?

- Scarface
Karsten
17
Years of Service
User Offline
Joined: 24th Jan 2007
Location: N/A
Posted: 23rd Aug 2007 00:05
Quote: "is there a GDK version?"


I'm guessing u mean .NET version. Yes there is and it is excellent because you can use the Microsoft Visual Studio to design forms and menus and stuff and then pass variables etc into the engine within one program. You can also use .NET sockets for network code etc...

This is all possible with the normal GDK but in my opinion .NET is by far easier to use and quicker to develop for. You do pay the price slightly with speed but my code is so crap anyways that you wouldn't be able to tell the difference... (refer to vanilla collision surce code for proof :p)

You get it from the same place as the normal (c++) GDK.
Scarface
18
Years of Service
User Offline
Joined: 21st Oct 2006
Location:
Posted: 23rd Aug 2007 00:10
No Karsten, I meant is there a DarkGDK version of that function, not .Net as I am not using that version.

Cheers.

- Scarface
Red Ocktober
20
Years of Service
User Offline
Joined: 6th Dec 2003
Location:
Posted: 23rd Aug 2007 00:19
just write your own function Scar... you have the code right there... then you can use it over and over again when you need it...

--Mike
Karsten
17
Years of Service
User Offline
Joined: 24th Jan 2007
Location: N/A
Posted: 23rd Aug 2007 00:30
Lol, I see. OK, sorry for my shameless advertising.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Aug 2007 02:12
Change

Quote: "sprintf(sX, "f%", fX);"


to

Quote: "sprintf(sX, "%f", fX);"


You had the percent on the wrong side of the 'f'.

Stick with sprintf instead of the db functions, they create way too many extra pointers and things.

Cheers,
Ancient Lady
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 13th Sep 2007 02:56
Good advice Ancient Lady.

Also - what ancient Lady recommended is probably along the lines of how I TRY to write code. To use as little overhead as possible - (Unless I'm in a particularly lazy mood or am under the gun for a deadline) - well... I guess everyone knows this.

If you have the C++ version - speed is likely very important to you. If you'll sacrifice some speed for rapid application development via "Staples Easy Button" than that works two.

But to topic - you might want your "Text buffer" placed somewhere global now and used everywhere (not multi-thread safe - and you have to be careful about using it twice via function uses it that calls a function that uses it etc) But this gives the most extreme speed - and least resources.

For threadsafe and function safe - do the stack variety which is declare it in your function itself.

another option for speed and resource thiftyness is to make the float to string thing INLINE and make a global text buffer ONLY used by THAT inline function. you inlines add a tiny bit your exe size - but the speed and resources used - well - FAST and LIGHT.

Good Luck

Login to post a reply

Server time is: 2024-11-16 20:45:49
Your offset time is: 2024-11-16 20:45:49