求教时间格式转换问题

汇可8 2019-07-27 12:52:40
QString time ="13:26:56";
float d=time.toFloat();

d的值为什么是0.000000?
...全文
188 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
汇可8 2019-08-11
  • 打赏
  • 举报
回复
对的是什么呢?能给段完整的代码实现这个转换吗?
汇可8 2019-08-09
  • 打赏
  • 举报
回复
头文件是 stdio.h 吗?
lin5161678 2019-08-09
  • 打赏
  • 举报
回复
引用 11 楼 ScSofts 的回复:
很简单 头文件stdio.h char a[3],b[3],c[3]; sscanf(time,"%s:%s:%s",a,b,c); char fs[20]; sprintf(fs,"0.%s%s%s",a,b,c); float f = atof(fs);
错的 %s 全读走了 %s是遇到空白字符停止 : 属于%s的处理字符
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>

float TimeToFloat(const char *time)
{
int h, m, s;

sscanf(time, "%d:%d:%d", &h, &m, &s);

return fmod(h / 100.0, 1) +
fmod(m / 100.0, 1) / 100 +
fmod(s / 100.0, 1) / 10000;
}
ScSofts 2019-07-28
  • 打赏
  • 举报
回复
很简单 头文件stdio.h char a[3],b[3],c[3]; sscanf(time,"%s:%s:%s",a,b,c); char fs[20]; sprintf(fs,"0.%s%s%s",a,b,c); float f = atof(fs);
汇可8 2019-07-27
  • 打赏
  • 举报
回复
把时间"13:26:56"转为0.132656就这简单需求。没什么奇葩的!
赵4老师 2019-07-27
  • 打赏
  • 举报
回复
引用 7 楼 汇可8 的回复:
只要把时间"13:26:56"转为0.132656

奇葩需求!
yshuise 2019-07-27
  • 打赏
  • 举报
回复
用C语言直接解析,会不会?当然调用库函数更简单
汇可8 2019-07-27
  • 打赏
  • 举报
回复
只要把时间"13:26:56"转为0.132656
汇可8 2019-07-27
  • 打赏
  • 举报
回复
int ktime=QDateTime::currentDateTime().toTime_t();
取当前系统时间是可以的,但我需要的是转换过去的一个特定的时间.
boot_black 2019-07-27
  • 打赏
  • 举报
回复
先把时间转换为时间戳,时间戳肯定是数值的,也是是相对于1970年1月1日0时0分0秒所经过的秒数。
汇可8 2019-07-27
  • 打赏
  • 举报
回复
怎么转成可以计算时间差的格式呢?
唐诗三百首 2019-07-27
  • 打赏
  • 举报
回复
因为"13:26:56"不是有效的数值, 转float失败, 所以返回0.
唐诗三百首 2019-07-27
  • 打赏
  • 举报
回复
float QString::toFloat(bool *ok = nullptr): Returns the string converted to a float value. Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow). If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true. Warning: The QString content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error. The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toFloat() For historical reasons, this function does not handle thousands group separators. If you need to convert such numbers, use QLocale::toFloat(). 参考 https://doc.qt.io/qt-5/qstring.html#toFloat

64,637

社区成员

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

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