wsprintf的问题?

register_jhb 2007-02-13 03:25:26
如何在格式化字串中加入浮点类型数据? 如
TCHAR str[88];
float temp = 3.1415;
wsprintf(str, L"%f", temp);

这样得到的str的内容是字符串"f", 而不是想得到的"3.1415"字串.
怎样在wsprintf中格式化浮点数为一个字串?
...全文
987 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinglonng 2012-05-17
  • 打赏
  • 举报
回复
wsprintf 并不是 sprintf 的 Unicode 版本。
wsprintf 是 Windows 版的 sprintf,前面的 w 是 Windows 意思。
wsprintf 宏定义的两个版本分别为 wsprintfA、wsprintfW,均不支持 %f。
要支持 %f,需使用 sprintf 及其 Unicode 版 swprintf,通用宏定义 _stprintf。
yevv 2007-02-14
  • 打赏
  • 举报
回复
你可以用sprintf来实现此目的
sprintf(str,"%f",3.1415);
yevv 2007-02-14
  • 打赏
  • 举报
回复
wsprintf 不支持该格式化浮点型

它支持如下格式化字符
Output the corresponding argument as a character, a string, or a number. This field can be any of the following values. Value Meaning
c Single character. This value is interpreted as type WCHAR if the calling application defines Unicode and as type __wchar_t otherwise.
C Single character. This value is interpreted as type __wchar_t if the calling application defines Unicode and as type WCHAR otherwise.
d Signed decimal integer. This value is equivalent to i.
hc, hC Single character. The wsprintf function ignores character arguments with a numeric value of zero. This value is always interpreted as type __wchar_t, even when the calling application defines Unicode.
hd Signed short integer argument.
hs, hS String. This value is always interpreted as type LPSTR, even when the calling application defines Unicode.
hu Unsigned short integer.
i Signed decimal integer. This value is equivalent to d.
lc, lC Single character. The wsprintf function ignores character arguments with a numeric value of zero. This value is always interpreted as type WCHAR, even when the calling application does not define Unicode.
ld Long signed integer. This value is equivalent to li.
li Long signed integer. This value is equivalent to ld.
ls, lS String. This value is always interpreted as type LPWSTR, even when the calling application does not define Unicode. This value is equivalent to ws.
lu Long unsigned integer.
lx, lX Long unsigned hexadecimal integer in lowercase or uppercase.
p Windows 2000/XP: Pointer. The address is printed using hexadecimal.
s String. This value is interpreted as type LPWSTR when the calling application defines Unicode and as type LPSTR otherwise.
S String. This value is interpreted as type LPSTR when the calling application defines Unicode and as type LPWSTR otherwise.
u Unsigned integer argument.
x, X Unsigned hexadecimal integer in lowercase or uppercase
du51 2007-02-13
  • 打赏
  • 举报
回复
#include<windows.h>
#include<tchar.h>
#include<stdio.h>
#define UNICODE
int main()
{
TCHAR str[100];
float f=3.1415926f;
_sntprintf(str,100,TEXT("%f"),f);
MessageBox(NULL,str,TEXT("测试wsprintf"),MB_OK);
return 0;
}
lockhall 2007-02-13
  • 打赏
  • 举报
回复
ls正解.

#include<stdio.h>
#include <stdlib.h>

int main(void)
{
char str[88];
float temp = 3.1415;
sprintf(str, "%f", temp);
printf("%s",str);
getch();
return 0;
}
羽盛 2007-02-13
  • 打赏
  • 举报
回复
用sprintf即可

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧