c语言 字段宽度 浮点,C 浮点类型

学习C - C浮点类型

浮点数保存用小数点写入的值。

以下是浮点值的示例: 1.6 0.00018 1234.123 100.0

最后一个常数是整数,但它将被存储为浮点值。

浮点数通常表示为十进制值乘以10的幂,其中10的幂称为指数。

下表显示了如何显示浮点数。值用C写成1.70.17E1

0.000090.9E-4

7655.1230.7655123E4

100.01.0E2

/* your weight in platinum */

#include

int main(void)

{

float weight; /* user weight */

float value; /* platinum equivalent */

printf("Please enter your weight in pounds: ");

/* get input from the user */

scanf("%f", &weight);

value = 3.0 * weight;

printf("Your weight is worth $%.2f.\n", value);

return 0;

}

上面的代码生成以下结果。

83ef5c8b1a9ceccc8369f8fcfb855e70.png

浮点变量

您可以选择三种类型的浮点变量,如下表所示。关键词字节数值范围float4+/- 3.4E(+/- 38)

double8+/-1.7E(+/-308)

long double12+/- 1.19E(+/- 4932)

以下代码定义了两个浮点数。 float radius;

double biggest;

要写一个float类型的常量,你需要附加一个f到数字,以便将它与double类型区分开来。

当你这样声明它们时,你可以初始化前两个变量: float radius = 2.5f;

double biggest = 123E30;

可变半径的初始值为2.5,变量最大值被初始化为对应于123的数字,接着是30个零。

要指定long double常数,请附加大写或小写字母L. long double huge = 1234567.98765L;

以下代码显示了如何浮动类型变量声明和初始化。

#include

main()

{

//variable declarations float y;

//variable initializations y = 554.21;

//printing variable contents to standard output printf("\nThe value of float variable y is %f", y);

}

上面的代码生成以下结果。

27e5c09969c8a2ec418bc5cbb4d22f53.png

使用浮点值进行分割

这是一个简单的例子,它将一个浮点值除以另一个,并输出结果:

#include

int main(void)

{

float length = 10.0f;

float part_count = 4.0f; // Number of equal pieces float part_length = 0.0f;

part_length = length/part_count;

printf("%f can be divided into %f pieces and each part is f .\n", length, part_count, part_length);

return 0;

}

上面的代码生成以下结果。

696f9d6f2b5d008bf79b12c46e15fe48.png

小数点数

您可以在格式说明符中指定要在小数点后查看的地点数。

要将输出获得两位小数,您可以将格式说明符写为%.2f。

要获取三位小数,您可以写入%.3f。

以下代码更改printf()语句,以便它将产生更合适的输出:

#include

int main(void)

{

float length = 10.0f;

float part_count = 4.0f; // Number of equal pieces float part_length = 0.0f;

part_length = length/part_count;

printf("A plank %.2f feet long can be cut into %.0f pieces %.2f feet long.\n",

length, part_count, part_length);

return 0;

}

上面的代码生成以下结果。

af2d8b3559202ce8911fdb3ffc306fb6.png

输出字段宽度

您可以指定字段宽度和小数位数。

浮点值格式说明符的一般形式可以这样写: %[width][.precision][modifier]f

方括号表示可选。

您可以省略宽度或精度或修饰符或这些的任何组合。

width值是一个整数,指定输出中包含空格的字符总数。它是输出字段的宽度。

precision值是指定小数点后的小数位数的整数。

当您输出的值为long double时,修饰符部分为L,否则省略。

#include

int main(void)

{

float length = 10.0f;

float part_count = 4.0f; // Number of equal pieces float part_length = 0.0f;

part_length = length/part_count;

printf("A %8.2f plank foot can be cut into %5.0f pieces %6.2f feet long.\n",

length, part_count, part_length);

const double RENT = 3852.99; // const-style constant

printf("*%f*\n", RENT);

printf("*%e*\n", RENT);

printf("*%4.2f*\n", RENT);

printf("*%3.1f*\n", RENT);

printf("*%10.3f*\n", RENT);

printf("*%10.3E*\n", RENT);

printf("*%+4.2f*\n", RENT);

printf("*%010.2f*\n", RENT);

return 0;

}

上面的代码生成以下结果。

43568a59871c2fda3571230238ce40b7.png

精度

要使用浮点数创建精度,请使用%符号和f字符转换说明符之间的编号方案调整转换说明符。

#include

int main(void)

{

printf("%.1f", 3.123456);

printf("\n%.2f", 3.123456);

printf("\n%.3f", 3.123456);

printf("\n%.4f", 3.123456);

printf("\n%.5f", 3.123456);

printf("\n%.6f", 3.123456);

return 0;

}

上面的代码生成以下结果。

ac79394d01f92bfa678fe960a493231a.png

左对齐

指定字段宽度时,默认情况下,输出值将对齐。

如果您希望该值在字段中保持对齐,则只需在%之后放置一个减号。

#include

int main(void)

{

float length = 10.0f;

float part_count = 4.0f; // Number of equal pieces float part_length = 0.0f;

part_length = length/part_count;

printf("A %-18.2f plank foot can be cut into %5.0f pieces %6.2f feet long.\n",

length, part_count, part_length);

return 0;

}

您可以在字段中指定字段宽度和对齐方式,并输出整数值。

例如,%-15d指定一个整数值将被显示为左对齐的字段宽度为15个字符。

上面的代码生成以下结果。

8143ad0e1e86b8ddba8172b2386cb952.png

例子

以下代码显示如何计算圆形表的圆周和面积。

#include

int main(void)

{

float radius = 0.0f; // The radius of the table float diameter = 12.12f; // The diameter of the table float circumference = 0.0f; // The circumference of the table float area = 0.0f; // The area of the table float Pi = 3.14159265f;

radius = diameter/2.0f; // Calculate the radius circumference = 2.0f*Pi*radius; // Calculate the circumference area = Pi*radius*radius; // Calculate the area

printf("\nThe circumference is %.2f", circumference);

printf("\nThe area is %.2f\n", area);

return 0;

}

上面的代码生成以下结果。

2b7b8c5da652b10cae7c3fbf7d0e0ec3.png

例2

以下代码使用来自limit.h和float的定义的常量。

#include

#include // integer limits#include // floating-point limitsint main(void)

{

printf("Some number limits for this system:\n");

printf("Biggest int: %d\n", INT_MAX);

printf("Smallest long long: %lld\n", LLONG_MIN);

printf("One byte = %d bits on this system.\n", CHAR_BIT);

printf("Largest double: %e\n", DBL_MAX);

printf("Smallest normal float: %e\n", FLT_MIN);

printf("float precision = %d digits\n", FLT_DIG);

printf("float epsilon = %e\n", FLT_EPSILON);

return 0;

}

上面的代码生成以下结果。

b92ca3dfee66d8dc8920408b3c2ac9d9.png

例3

以下代码以两种方式显示浮点值。

#include

int main(void) {

float aboat = 32000.0;

double abet = 2.14e9;

long double dip = 5.32e-5;

printf("%f can be written %e\n", aboat, aboat);

// next line requires C99 or later compliance printf("And it"s %a in hexadecimal, powers of 2 notation\n", aboat);

printf("%f can be written %e\n", abet, abet);

printf("%Lf can be written %Le\n", dip, dip);

return 0;

}

上面的代码生成以下结果。

ca2009015160c2983032e8152a33bd52.png

例4

读取命令行的浮点型号。

#include

#include // for strlen() prototype #define DENSITY 62.4 // human density in lbs per cu ft int main()

{

float weight, volume;

int size, letters;

char name[40]; // name is an array of 40 chars

printf("Hi! What"s your first name?\n");

scanf("%s", name);

printf("%s, what"s your weight in pounds?\n", name);

scanf("%f", &weight);

size = sizeof name;

letters = strlen(name);

volume = weight / DENSITY;

printf("%s, your volume is %2.2f cubic feet.\n", name, volume);

printf("first name has %d letters,\n", letters);

printf("we have %d bytes to store it.\n", size);

return 0;

}

上面的代码生成以下结果。

a475312fb34d5bb84fd0ca76b377e65a.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值