问题描述
您好,
我想打印浮点数而不留尾零。
我尝试使用%g,但现在小数字是以%e
样式打印(如果转换中的指数为
小于-4,则使用样式e)。有没有办法控制这个数字(-4)?
I.e.我可以得到printf打印像0.0000001,但没有
尾随零?或者我应该手工完成这个吗?
谢谢,
Mandar。
Hello,
I''d like to print floating point numbers without trailing zeros.
I tried using %g, but now small numbers are printed in the %e
style ("Style e is used if the exponent from its conversion is
less than -4"). Is there some way to control this number (-4)?
I.e. can I get printf to print something like 0.0000001, but no
trailing zeros? Or should I do this by hand?
Thanks,
Mandar.
推荐答案
我认为你的意思是你想要0.0100000打印为0.01。并且
0.00001000打印为0.00001。
您需要滚动自己的代码。你首先必须解决
有多少小数是重要的,然后通过sprintf创建一个格式字符串
,然后printf这个数字。 />
计算出有多少小数是非常有趣的。
-
马克麦金太尔
CLC FAQ< http ://www.eskimo.com/~scs/C-faq/top.html>
CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt> ;
---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
世界排名第一的新闻组服务! 120,000多个新闻组
---- =东海岸和西海岸服务器农场 - 通过加密实现全面隐私= ----
I presume you mean you want 0.0100000 to print as "0.01" and
0.00001000 to print as "0.00001".
You need to roll your own code. You''d first of all have to work out
how many of the decimals were significant, then create a format string
via sprintf, then printf the number.
Working out how many decimals are significant is quite interesting.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
我认为你的意思是你想要0.0100000打印为0.01。和
0.00001000打印为0.00001。
您需要滚动自己的代码。你首先必须解决
有多少小数是重要的,然后通过sprintf创建一个格式字符串
,然后printf数字。
计算出多少小数是重要的是非常有趣。
I presume you mean you want 0.0100000 to print as "0.01" and
0.00001000 to print as "0.00001".
You need to roll your own code. You''d first of all have to work out
how many of the decimals were significant, then create a format string
via sprintf, then printf the number.
Working out how many decimals are significant is quite interesting.
可能最简单的方法是简单地将其截断为给定的
金额,比如说,{FLT,DBL ,LDBL} _DIG然后在那里切断尾随零。
更准确但更慢的方法是尝试每个数字位数
依次查看此类比较的结果是否等于
原件。
Probably the easiest way would be to simply truncate it to a given
amount, say, {FLT,DBL,LDBL}_DIG and then cut off trailing zeros from
there.
A more exact, but slower, way, would be to try each amount of digits
in turn and see if the result of such compares equal to the
original.
对。你想打印.20000为.2但打印
..19999为.19999。这是一个非常有趣的事情,
因此没有内置的方法。
您需要手动完成。这是一种方式
-保存标志并转换为绝对值
- 保存数字的整数部分和
将它们减少到少于1.
- 将您的数字四舍五入到您想要的最大值
位数。
- sprintf将数字放入缓冲区,指定
最大位数
- 删除尾随零(或替换为空白
如果你想要恒定的字段宽度)
- sprintf整数部分,然后strcat小数
部分(从第二个角色丢失
零)
-将标志重新添加到前面,(并填充空白
如果你想要恒定的场宽,那就在前面。
通常情况下,.2,.20,。200意味着不同的东西,所以压抑了b / b
尾随的零通常是不好的。
-William Hughes
Right. You want to print .20000 as .2 but print
..19999 as .19999. This is a pretty stuipid thing to do,
so there is no built in way of doing it.
You need to do it by hand. Here''s one way
-save the sign and convert to absolute value
-save the integer part of the numbers and
reduce them to less than 1.
- Round off your numbers to your desired maximum
number of digits.
- sprintf the number into a buffer, specifiying
the maximum number of digits
- remove trailing zeros (or replace with blanks
if your want constant field width)
- sprintf the integer parts, then strcat the fractional
parts (starting at the second character to lose
the zero)
-add the sign back to the front, (and pad with blanks
at the front if you want constant field width)
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.
-William Hughes
这篇关于如何用printf获得0.000001?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!