想取double变量里面的实际长度,怎么取啊。。。HELP....

duzhonghua 2012-02-20 10:05:24

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(argc, argv)
{
double cc;
cc = 1234569100123.123;
}

想取cc的实际长度 ,预想是17
怎么取??

试过转成字符串再取长度,但发现把cc转成字符串后,会自动补0,导致我取到的长度是20

麻烦大家帮忙看看!!
...全文
386 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-02-20
  • 打赏
  • 举报
回复
Precision Specification
The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table R.5). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:

printf( "%.0d", 0 ); /* No characters output */

If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list.

The type determines the interpretation of precision and the default when precision is omitted, as shown in Table R.5.

Table R.5 How Precision Values Affect Type

Type Meaning Default
c, C The precision has no effect. Character is printed.
d, i, u, o, x, X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. Default precision is 1.
e, E The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed.
f The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.
g, G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated.
s, S The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered.

AnYidan 2012-02-20
  • 打赏
  • 举报
回复
while(len > 0 && str[len-1] == '0')
AnYidan 2012-02-20
  • 打赏
  • 举报
回复

char str[32] = {0};
double cc;
int len = 0;

cc = 1234569100123.123;

sprintf(str, "%f", cc);

len = strlen(str);
while(str[len-1] == '0')
{
str[len-1] = '\0';
len--;
}

printf("%d\n", len);

system("pause");
return 0;


偷师 1 楼,莫怪!
nice_cxf 2012-02-20
  • 打赏
  • 举报
回复
1楼的做法是把double转成float输出,损失精度,并且一样有误差,因为没有办法消除误差,可以说你的问题基本无解
赵4老师 2012-02-20
  • 打赏
  • 举报
回复
用10进制小数不能精确表示某些三进制小数0.1(3)=0.33333333333……(10)
同理,用二进制小数也不能精确表示某些10进制小数。
赵4老师 2012-02-20
  • 打赏
  • 举报
回复
printf("%.15lg\n",cc);
参考
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\float.h
...
#define DBL_DIG 15 /* # of decimal digits of precision */
#define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
#define DBL_MANT_DIG 53 /* # of bits in mantissa */
#define DBL_MAX 1.7976931348623158e+308 /* max value */
#define DBL_MAX_10_EXP 308 /* max decimal exponent */
#define DBL_MAX_EXP 1024 /* max binary exponent */
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */
#define DBL_MIN_10_EXP (-307) /* min decimal exponent */
#define DBL_MIN_EXP (-1021) /* min binary exponent */
#define _DBL_RADIX 2 /* exponent radix */
#define _DBL_ROUNDS 1 /* addition rounding: near */
...
duzhonghua 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 czh3642210 的回复:]
我试了下别的方法都有误差,这个就不错。。。
[/Quote]

行我试试。。
duzhonghua 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 nice_cxf 的回复:]
double是不一定可以精确表示某个数值的,因此你的想法和最终结果可能相差很大,除非你用5楼那样格式化输出,不过我估计那不是你想要的
[/Quote]

是的,呵呵
duzhonghua 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cxsjabcabc 的回复:]
C/C++ code



int main()
{
char buf[32];
double cc;
cc = 1234569100123.123;
sprintf(buf, "%17.3f", cc);
cout << buf;

return 0;
}
[/Quote]

这个方法不是我想要的,因为你固定了小数点的位数,但我的小数点的位数不固定的。。
nice_cxf 2012-02-20
  • 打赏
  • 举报
回复
double是不一定可以精确表示某个数值的,因此你的想法和最终结果可能相差很大,除非你用5楼那样格式化输出,不过我估计那不是你想要的
一丝晨光 2012-02-20
  • 打赏
  • 举报
回复


int main()
{
char buf[32];
double cc;
cc = 1234569100123.123;
sprintf(buf, "%17.3f", cc);
cout << buf;

return 0;
}

IVERS0N 2012-02-20
  • 打赏
  • 举报
回复
你既然取到了20 就把指针指向20的位置

指针往前走 遇到0就写'\0'或者统计下连续0的长度
面包大师 2012-02-20
  • 打赏
  • 举报
回复
我试了下别的方法都有误差,这个就不错。。。
qq120848369 2012-02-20
  • 打赏
  • 举报
回复
浮点数小数点后有很多位, 你没发控制它, 只能自己决定舍入到哪一位.
面包大师 2012-02-20
  • 打赏
  • 举报
回复
	double cc;
int i,j = 0, k = 0;
cc = 1234569100123.123;
char str[32] = {0};
sprintf(str, "%f", cc);

for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == '0')
{
j++;
}
else
{
k++;
k+=j;
j=0;
}
}

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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