字符串转float或double

wellxia 2016-02-02 02:18:21
现在有一个浮点值串str, 可能为多字节或unicode编码;

想要实现的功能如下:
1. 把值串转换为float值, 并且判断转换是否溢出或超出精度范围;
2. 如果这个值串能用浮点数表示,则将其转换为float, 否则转换为double值。



微软的_atoflt()函数貌似可以实现我的功能, 但是值串只能为多字节编码的, unicode则不行,

请大侠点拨, Thanks~
...全文
246 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2016-02-08
  • 打赏
  • 举报
回复
stringstream
yinjor 2016-02-04
  • 打赏
  • 举报
回复
_atoflt(),sprintf
赵4老师 2016-02-03
  • 打赏
  • 举报
回复
2.0版:
#include <stdio.h>
#include <string.h>
void strwstr2doublefloat(char *str,float **ppf,double **ppd) {
    int n;
    static double d;
    static float f;
    char sf[30],sd[30];
    int flag;

    *ppf=NULL;
    *ppd=NULL;
    flag=0;
    while (1) {
        if (1==swscanf((wchar_t *)str,L"%lf%n",&d,&n)) {
            if (n==(int)wcslen((wchar_t *)str)) {
                flag=1;
                break;
            }
        }
        if (1==sscanf(str,"%lf%n",&d,&n)) {
            if (n==(int)strlen(str)) {
                flag=1;
                break;
            }
        } else {
            break;
        }
    }
    if (flag) {
        f=(float)d;
        sprintf(sd,"%.15lg",d);
        sprintf(sf,"%.6g",f);
        if (NULL==strchr(sd,'#')) {
            if (0==strcmp(sd,sf)) {
                *ppf=&f;
            } else {
                *ppd=&d;
            }
        } else {
            ;
            //If the argument corresponding to a floating-point specifier is
            //infinite, indefinite, or NaN, printf gives the following output.
            //Value                           Output
            //------------------------------  -----------------------
            //+infinity                       1.#INFrandom-digits
            //-infinity                       -1.#INFrandom-digits
            //Indefinite (same as quiet NaN)  digit.#INDrandom-digits
            //NAN                             digit.#NANrandom-digits
        }
    }
}
int main() {
    char s1[]="2e400";
    wchar_t s2[]=L"1e39";
    float *pf;
    double *pd;

    strwstr2doublefloat((char *)s1,&pf,&pd);
    if (!pf&&!pd) printf("Format error!\n");
    if (pf) printf("float %g\n",*pf);
    if (pd) printf("double %lg\n",*pd);

    strwstr2doublefloat((char *)s2,&pf,&pd);
    if (!pf&&!pd) printf("Format error!\n");
    if (pf) printf("float %.6g\n",*pf);
    if (pd) printf("double %.15lg\n",*pd);

    return 0;
}
赵4老师 2016-02-02
  • 打赏
  • 举报
回复
作为一个C程序员,对 scanf,sscanf,fscanf printf,sprintf,fprintf 这类函数的用法,还是要做到“拳不离手,曲不离口”的。
#include <stdio.h>
#include <string.h>
void strwstr2doublefloat(char *str,float **ppf,double **ppd) {
    int n;
    static double d;
    static float f;
    char sf[30],sd[30];
    int flag;

    *ppf=NULL;
    *ppd=NULL;
    flag=0;
    while (1) {
        if (1==swscanf((wchar_t *)str,L"%lf%n",&d,&n)) {
            if (n==(int)wcslen((wchar_t *)str)) {
                flag=1;
                break;
            }
        }
        if (1==sscanf(str,"%lf%n",&d,&n)) {
            if (n==(int)strlen(str)) {
                flag=1;
                break;
            }
        } else {
            break;
        }
    }
    if (flag) {
        f=(float)d;
        sprintf(sd,"%.15lg",d);
        sprintf(sf,"%.6g",f);
        if (0==strcmp(sd,sf)) {
            *ppf=&f;
        } else {
            *ppd=&d;
        }
    }
}
int main() {
    char s1[]="1.2";
    wchar_t s2[]=L"1.23456789012";
    float *pf;
    double *pd;

    strwstr2doublefloat((char *)s1,&pf,&pd);
    if (!pf&&!pd) printf("Format error!\n");
    if (pf) printf("float %g\n",*pf);
    if (pd) printf("double %lg\n",*pd);

    strwstr2doublefloat((char *)s2,&pf,&pd);
    if (!pf&&!pd) printf("Format error!\n");
    if (pf) printf("float %.6g\n",*pf);
    if (pd) printf("double %.15lg\n",*pd);

    return 0;
}
//float 1.2
//double 1.23456789012
//
paschen 版主 2016-02-02
  • 打赏
  • 举报
回复
那你就先把字符串转换成多字节的
wellxia 2016-02-02
  • 打赏
  • 举报
回复
在线等回复啊, 顶起!

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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