浮点数转字符串的问题

你怎么了熊吉 2013-07-16 02:59:30
我现在用QString::number(double)转,但是有个问题,比如输入100.00,输出的就是100,我想强制保留小数点后两位,应该用什么函数?
...全文
144 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xcyl 2013-07-16
  • 打赏
  • 举报
回复
引用 4 楼 czarten 的回复:
[quote=引用 1 楼 cai6811376 的回复:] QString::number(yourDouble, '.', 2);
真的可以! 求指教,第二个参数它给的选项不是'e','E','f','G'这些吗??[/quote] 其实改用QString::number(yourDouble, 'f', 2);的
你怎么了熊吉 2013-07-16
  • 打赏
  • 举报
回复
引用 1 楼 cai6811376 的回复:
QString::number(yourDouble, '.', 2);
真的可以! 求指教,第二个参数它给的选项不是'e','E','f','G'这些吗??
你怎么了熊吉 2013-07-16
  • 打赏
  • 举报
回复
引用 2 楼 q245879 的回复:

QString QString::number(double n, char format = 'g', int precision = 6) [static]
Returns a string equivalent of the number n, formatted according to the specified format and precision. See Argument Formats for details.

Unlike QLocale::toString(), this function does not honor the user's locale settings.

See also setNum() and QLocale::toString().

Argument Formats

In member functions where an argument format can be specified (e.g., arg(), number()), the argument format can be one of the following:

Format	Meaning
e	format as [-]9.9e[+|-]999
E	format as [-]9.9E[+|-]999
f	format as [-]9.9
g	use e or f format, whichever is the most concise
G	use E or f format, whichever is the most concise
A precision is also specified with the argument format. For the 'e', 'E', and 'f' formats, the precision represents the number of digits after the decimal point. For the 'g' and 'G' formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).

关键是第二个参数应该传什么,如果是默认的g是得不到你要的结果的,第三个参数是你想要保留的小数点后面的位数
第三个参数不是有效数字吗? 第二个我就是这个列表看不明白,其它的选项好像是科学计数法吧?
_Gx 2013-07-16
  • 打赏
  • 举报
回复

QString QString::number(double n, char format = 'g', int precision = 6) [static]
Returns a string equivalent of the number n, formatted according to the specified format and precision. See Argument Formats for details.

Unlike QLocale::toString(), this function does not honor the user's locale settings.

See also setNum() and QLocale::toString().

Argument Formats

In member functions where an argument format can be specified (e.g., arg(), number()), the argument format can be one of the following:

Format	Meaning
e	format as [-]9.9e[+|-]999
E	format as [-]9.9E[+|-]999
f	format as [-]9.9
g	use e or f format, whichever is the most concise
G	use E or f format, whichever is the most concise
A precision is also specified with the argument format. For the 'e', 'E', and 'f' formats, the precision represents the number of digits after the decimal point. For the 'g' and 'G' formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).

关键是第二个参数应该传什么,如果是默认的g是得不到你要的结果的,第三个参数是你想要保留的小数点后面的位数
xcyl 2013-07-16
  • 打赏
  • 举报
回复
QString::number(yourDouble, '.', 2);

21,489

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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