不需要的舍入 [英] Unwanted rounding
问题描述
你好,
我有:
float f = 36.09999999;
当我这样做时: br />
char cf [25];
sprintf(cf,"%0.03lf",f);
我得到: 36.100
我怎么能得到36.099?
提前致谢。
Marco< .said:
你好,
我有:
float f = 36.09999999;
我建议使用double而不是float。
>
我这样做:
char cf [25];
sprintf(cf,"%0.03lf",f);
不是lf - 只是f。
>
我得到:36.100
我怎么能得到36.099?
删除精度规范(03),然后使用strchr在字符串中搜索
小数点。检查小数点后至少有三个有效的
字符(非''\\'0''),并使第四个字符''\ 0''
在你想要的位置截断字符串。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
Marco写道:
你好,
我有:
float f = 36.09999999;
当我这样做时:
char cf [25];
sprintf(cf,"%0.03lf",f);
我得到:36.100
我怎么能得到36.099?
ma **** *****@pobox.com 说:
>
Marco写道:
< blockquote class =post_quotes>
>你好,
我有:
浮动f = 36.09999999;
当我这样做时:
char cf [25];
sprintf(cf,"%0.03lf",f);
我得到:36.100
我怎么能得到36.099 ?
他可以。请参阅我的平行回复。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
Hello,
I have :
float f = 36.09999999;
When I do :
char cf[25];
sprintf(cf,"%0.03lf", f);
I get : 36.100
How could I get 36.099 ?
Thanks in advance.
Marco <.said:
Hello,
I have :
float f = 36.09999999;I recommend double rather than float.
>
When I do :
char cf[25];
sprintf(cf,"%0.03lf", f);Not lf - just f.
>
I get : 36.100
How could I get 36.099 ?Remove the precision specification (03), and then search the string for the
decimal point, using strchr. Check that you have at least three valid
characters (non-''\0'') after the decimal point, and make the fourth one ''\0''
to truncate the string at the point you want.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Marco wrote:Hello,
I have :
float f = 36.09999999;
When I do :
char cf[25];
sprintf(cf,"%0.03lf", f);
I get : 36.100
How could I get 36.099 ?You can''t. See http://c-faq.com/fp/printfprec.html
ma**********@pobox.com said:
>
Marco wrote:>Hello,
I have :
float f = 36.09999999;
When I do :
char cf[25];
sprintf(cf,"%0.03lf", f);
I get : 36.100
How could I get 36.099 ?
You can''t. See http://c-faq.com/fp/printfprec.htmlHe can. See my parallel reply.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于不需要的舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!