Board index » cppbuilder » Re: Need help with converting float to string

Re: Need help with converting float to string


2004-05-22 04:06:17 AM
cppbuilder79
sprintf(string, "%3.2f", data);
Regards,
Bruce
 
 

Re:Re: Need help with converting float to string

Jeff:
I tipically use sprintf:
data = (low_byte + 256 * (char) high_byte) / 100.0;
sprintf (string, "%f05.2", data);
Saludos
Sebastian
"Jeff Baker" < XXXX@XXXXX.COM >escribi?en el mensaje
Quote
I need some help. The function listed below compiles and works but the
format it displays is not the kind I need. This function displays "data"
in
exponential format and I need it to be in decimal format and no
suppression
of trailing zeros. The ecvt() and fcvt() functions do exactly what I'm
looking for, however neither of these functions insert the decimal in the
string. Does anyone have an idea on how to get this done?

Note: the data is alway in x.xx format.

[start of code]
#include <windows.h>
#include <math.h>

void DisplayData(HWND hwnd,BYTE low_byte,BYTE high_byte)
{
char string[255],label[]="Data";
float data;
HDC hdc;
RECT rect;
rect.left = 210; rect.top = 10; rect.right = 340; rect.bottom = 23;
HBRUSH back_color = (HBRUSH)GetStockObject(BLACK_BRUSH);
COLORREF text_color = RGB(127,255,127);
HFONT hfnt,hOldFont;
hdc = GetDC(hwnd);
SetTextColor(hdc,text_color);
SetBkMode(hdc,TRANSPARENT);
hfnt = (HFONT)GetStockObject(SYSTEM_FONT);
hOldFont =(HFONT)SelectObject(hdc,hfnt);
if (hOldFont)
{
FillRect(hdc,&rect,back_color);
TextOut(hdc,rect.left,rect.top,label,strlen(label));
data = (FLOAT)((low_byte + (256 * high_byte)) / pow10(2));
gcvt(data,4,string);
TextOut(hdc,rect.left+80,rect.top,string,strlen(string));
SelectObject(hdc,hOldFont);
}
UpdateWindow(hwnd);
ReleaseDC(hwnd,hdc);
}
[end of code]


 

Re:Re: Need help with converting float to string

I need some help. The function listed below compiles and works but the
format it displays is not the kind I need. This function displays "data" in
exponential format and I need it to be in decimal format and no suppression
of trailing zeros. The ecvt() and fcvt() functions do exactly what I'm
looking for, however neither of these functions insert the decimal in the
string. Does anyone have an idea on how to get this done?
Note: the data is alway in x.xx format.
[start of code]
#include <windows.h>
#include <math.h>
void DisplayData(HWND hwnd,BYTE low_byte,BYTE high_byte)
{
char string[255],label[]="Data";
float data;
HDC hdc;
RECT rect;
rect.left = 210; rect.top = 10; rect.right = 340; rect.bottom = 23;
HBRUSH back_color = (HBRUSH)GetStockObject(BLACK_BRUSH);
COLORREF text_color = RGB(127,255,127);
HFONT hfnt,hOldFont;
hdc = GetDC(hwnd);
SetTextColor(hdc,text_color);
SetBkMode(hdc,TRANSPARENT);
hfnt = (HFONT)GetStockObject(SYSTEM_FONT);
hOldFont =(HFONT)SelectObject(hdc,hfnt);
if (hOldFont)
{
FillRect(hdc,&rect,back_color);
TextOut(hdc,rect.left,rect.top,label,strlen(label));
data = (FLOAT)((low_byte + (256 * high_byte)) / pow10(2));
gcvt(data,4,string);
TextOut(hdc,rect.left+80,rect.top,string,strlen(string));
SelectObject(hdc,hOldFont);
}
UpdateWindow(hwnd);
ReleaseDC(hwnd,hdc);
}
[end of code]
 

{smallsort}

Re:Re: Need help with converting float to string

I need some help. The function listed below compiles and works but the
format it displays is not the kind I need. This function displays "data" in
exponential format and I need it to be in decimal format and no suppression
of trailing zeros. The ecvt() and fcvt() functions do exactly what I'm
looking for, however neither of these functions insert the decimal in the
string. Does anyone have an idea on how to get this done?
Note: the data is always in x.xx format.
[start of code]
#include <windows.h>
#include <math.h>
void DisplayData(HWND hwnd,BYTE low_byte,BYTE high_byte)
{
char string[255],label[]="Data";
float data;
HDC hdc;
RECT rect;
rect.left = 210; rect.top = 10; rect.right = 340; rect.bottom = 23;
HBRUSH back_color = (HBRUSH)GetStockObject(BLACK_BRUSH);
COLORREF text_color = RGB(127,255,127);
HFONT hfnt,hOldFont;
hdc = GetDC(hwnd);
SetTextColor(hdc,text_color);
SetBkMode(hdc,TRANSPARENT);
hfnt = (HFONT)GetStockObject(SYSTEM_FONT);
hOldFont =(HFONT)SelectObject(hdc,hfnt);
if (hOldFont)
{
FillRect(hdc,&rect,back_color);
TextOut(hdc,rect.left,rect.top,label,strlen(label));
data = (FLOAT)((low_byte + (256 * high_byte)) / pow10(2));
gcvt(data,4,string);
TextOut(hdc,rect.left+80,rect.top,string,strlen(string));
SelectObject(hdc,hOldFont);
}
UpdateWindow(hwnd);
ReleaseDC(hwnd,hdc);
}
[end of code]
 

Re:Re: Need help with converting float to string

Thanks for the quick reply. Your idea works in console programs but this is
a GUI with formatted output so I cant use any of the printf functions. Any
other ideas?
" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
Quote
sprintf(string, "%3.2f", data);

Regards,
Bruce



 

Re:Re: Need help with converting float to string

Never mind that, I see what you mean. It works! Thanks a bunch!
- Jeff Baker
"Jeff Baker" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks for the quick reply. Your idea works in console programs but this
is
a GUI with formatted output so I cant use any of the printf functions. Any
other ideas?

" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
news:40ae613a$ XXXX@XXXXX.COM ...
>sprintf(string, "%3.2f", data);
>
>Regards,
>Bruce
>
>
>


 

Re:Re: Need help with converting float to string

Thanks Sebastian!
"Sebastian Ledesma" <labo(no_sp@m)solidyne1.com>wrote in message
Quote
Jeff:

I tipically use sprintf:

data = (low_byte + 256 * (char) high_byte) / 100.0;
sprintf (string, "%f05.2", data);

Saludos
Sebastian

"Jeff Baker" < XXXX@XXXXX.COM >escribi?en el mensaje
news: XXXX@XXXXX.COM ...
>I need some help. The function listed below compiles and works but the
>format it displays is not the kind I need. This function displays "data"
in
>exponential format and I need it to be in decimal format and no
suppression
>of trailing zeros. The ecvt() and fcvt() functions do exactly what I'm
>looking for, however neither of these functions insert the decimal in
the
>string. Does anyone have an idea on how to get this done?
>
>Note: the data is alway in x.xx format.
>
>[start of code]
>#include <windows.h>
>#include <math.h>
>
>void DisplayData(HWND hwnd,BYTE low_byte,BYTE high_byte)
>{
>char string[255],label[]="Data";
>float data;
>HDC hdc;
>RECT rect;
>rect.left = 210; rect.top = 10; rect.right = 340; rect.bottom = 23;
>HBRUSH back_color = (HBRUSH)GetStockObject(BLACK_BRUSH);
>COLORREF text_color = RGB(127,255,127);
>HFONT hfnt,hOldFont;
>hdc = GetDC(hwnd);
>SetTextColor(hdc,text_color);
>SetBkMode(hdc,TRANSPARENT);
>hfnt = (HFONT)GetStockObject(SYSTEM_FONT);
>hOldFont =(HFONT)SelectObject(hdc,hfnt);
>if (hOldFont)
>{
>FillRect(hdc,&rect,back_color);
>TextOut(hdc,rect.left,rect.top,label,strlen(label));
>data = (FLOAT)((low_byte + (256 * high_byte)) / pow10(2));
>gcvt(data,4,string);
>TextOut(hdc,rect.left+80,rect.top,string,strlen(string));
>SelectObject(hdc,hOldFont);
>}
>UpdateWindow(hwnd);
>ReleaseDC(hwnd,hdc);
>}
>[end of code]
>
>