C++: displaying floats

Started by
6 comments, last by penetrator 20 years, 8 months ago
Hi, when i use a gcvt function to convert, for example, 0.02 to a char, when i display it i get "3.e-002". How can i print the 0.02 value instead ?
Advertisement
Try raising the num (second) parameter in gcvt
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
In C++:
#include <sstream>#include <string> ... float float_obj = 0.02f; std::stringstream ss;ss << float_obj;std::string float_str;ss >> float_str;

In C:
#include <stdio.h> ...  float float_obj = 0.02f; char float_str[5];sprint( float_str, "%f", float_obj );


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
and how would i use the float_str with a printf function in C++ ?

printf("%s\n",float_str);
daerid@gmail.com
thx for your help, i have a printing routine as follows:

GLvoid glPrint_IMG(GLint x, GLint y, char *string, int set) // Where The Printing Happens
{

glBindTexture(GL_TEXTURE_2D, texture_IMG[0]); // Select Our Font Texture
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base_IMG-32+(128*set)); // Sets The Base Character to 0
glCallLists(strlen(string), GL_BYTE, string); // Draws The Display List Text
glPopAttrib();

}

so i try to use it like this: glPrint_IMG(0,0,&float_str,0);

But i get the following error:
error C2664: ''glPrint_IMG'' : cannot convert parameter 3 from ''class std::basic_string,class std::allocat
or > *'' to ''char *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Not sure but this could work:
float_str.c_str()
el
yep, it worked using it like this:

const char * szWork = float_str.c_str();
glPrint_IMG(0,0,szWork,0);

thanks everybody for helping me !

This topic is closed to new replies.

Advertisement