的sprintf%G说明符给后点太少位数 [英] sprintf %g specifier gives too few digits after point

查看:822
本文介绍了的sprintf%G说明符给后点太少位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写浮点瓦尔到我的ini文件,我遇到了格式说明有问题。
我有一个浮点值,让它成为101.9716。现在我想将它写入我的ini文件,但问题是我还有一个浮点值,其中有少preceision(如15.85),和值在同一回路写ini文件。
所以我做的:

I'm trying to write floating point vars into my ini file and i encountered a problem with format specifiers. I have a float value, let it be 101.9716. Now i want to write it to my ini file, but the problem is i have another float values, which have less preceision (such as 15.85), and that values are writing to ini file in the same loop. so i do:

sprintf(valLineY, "%g", grade[i].yArr[j]);

我所有其它变量成为很好的字符,如20(如果它是20.00000),13.85(如果它是13.850000)等。但101.9716变成101.972出于某种原因。
你能告诉我为什么会发生这种情况,如何使其101.9716没有毁了我的思想(这是有关删除尾随零和不必要的perceisions)。
感谢您的帮助。

All my other variables become nice chars like "20" (if it was 20.00000), "13.85" (if it was 13.850000) and so on. But 101.9716 becomes "101.972" for some reason. Can you please tell me why does this happen and how to make it "101.9716" without ruining my ideology (which is about removing trailing zeroes and unneeded perceisions). Thanks for any help.

推荐答案

我测试:

double f = 101.9716;
printf("%f\n", f);
printf("%e\n", f);
printf("%g\n", f);

和它的输出:

101.971600
1.019716e+02 // Notice the exponent +02
101.972

下面就是C标准(N1570 7.21.6.1)说,关于转换说明先按g

Here's what C standard (N1570 7.21.6.1) says about conversion specifier g:

A 双击参数重新presenting一个浮点数的转换
  风格˚F电子(或风格˚F电子 G转换说明的情况下),根据不同的价值转换和precision。让我们的 P 的等于precision如果为零,6,如果省略了precision,或1如果precision为零。然后,如果有型有款电子转换将有一个指数的 X 的:

A double argument representing a floating-point number is converted in style f or e (or in style F or E in the case of a G conversion specifier), depending on the value converted and the precision. Let P equal the precision if nonzero, 6 if the precision is omitted, or 1 if the precision is zero. Then, if a conversion with style E would have an exponent of X:

- 如果 P>点¯x≥-4 的,转换风格˚F(或˚F)和 precision
  的对 - (X + 1)

— if P > X ≥ −4, the conversion is with style f (or F) and precision P − (X + 1).

- 否则,转换风格电子(或电子)和precision的对 - 1

— otherwise, the conversion is with style e (or E) and precision P − 1.

所以上面给出的 P 的将等于6,因为没有指定precision的,而 X 的将等于2,因为它是在风格指数电子

So given above, P will equal 6, because precision is not specified, and X will equal 2, because it's the exponent on style e.

公式的 6> 2> = -4 的因此真实的,风格<选择code>˚F。和 precision后会有的 6 - (2 + 1)= 3

Formula 6 > 2 >= -4 is thus true, and style f is selected. And precision will then be 6 - (2 + 1) = 3.

˚F,风格先按g 将去掉不必要的零,的 precision设置,即使的。

Unlike f, style g will strip unnecessary zeroes, even when precision is set.

最后,除非使用了标记,任何尾随零将从结果和小数点字符的小数部分去除如果有剩余的没有小数部分被去除。

Finally, unless the # flag is used, any trailing zeros are removed from the fractional portion of the result and the decimal-point character is removed if there is no fractional portion remaining.

所以设置足够高的precision:

So set high enough precision:

printf("%.8g\n", f);

打印:

101.9716

这篇关于的sprintf%G说明符给后点太少位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆