C语言 打印没有小数位的双精度数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6826556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing a double without the decimal places
提问by phoenix
I have a number stored as a doubleand want to print it without the decimal places. So if I had the double value 919545634521.000000, it is always printed with the decimal places added to it. How can I print it without it so it looks like: 919545634521?
我有一个存储为 a 的数字double,想打印它而不带小数位。所以如果我有 double value 919545634521.000000,它总是在打印时加上小数位。如果没有它,我如何打印它,使其看起来像:919545634521?
#include<stdlib.h>
int main()
{
double number = 9220343120;
printf("%??\n", number);
}
回答by Michael Krelin - hacker
try
尝试
printf("%.0lf\n",phoneNum);
you may also prefer
你也可能更喜欢
long long phoneNum;
phoneNum = strtoll(buffer,NULL,0);
printf("%lld\n",phoneNum);
instead. Depending on the system, though, you may need other function to convert (I think it's _strtoui64for windows).
反而。但是,根据系统的不同,您可能需要其他函数来转换(我认为它_strtoui64适用于 Windows)。
回答by NPE
Well, don't store the phone number as a floating-point number (one wrong move, and you'll end up with your telephone numbers getting roundedfor you).
好吧,不要将电话号码存储为浮点数(一个错误的举动,您最终会为您的电话号码四舍五入)。
Store it as an integer or a string.
将其存储为整数或字符串。
回答by Richard Holland
回答by jman
printf ("%.0f\n", phoneNum);should work.
printf ("%.0f\n", phoneNum);应该管用。
回答by caf
You're looking for precision- the format specifier %.0fshould do what you want.
您正在寻找精度- 格式说明符%.0f应该做你想做的。
回答by nevelis
Not sure about other compiler support, but GCC supports the 'g' format specifier, which will print a double using the least space required for full precision. For example:
不确定其他编译器是否支持,但 GCC 支持 'g' 格式说明符,它将使用全精度所需的最少空间打印双精度值。例如:
double d1 = 10.000;
double d2 = 25.03;
printf( "d1: %g\nd2: %g\n", d1, d2 );
will result in:
将导致:
d1: 10
d2: 25.03

