当前位置:网站首页>Input and output of floating point data (C language)

Input and output of floating point data (C language)

2022-07-07 08:29:00 It's a dream, it's you!

Catalog

1 Input of floating point data

1.1  Single precision input

1.2  Double precision and long double precision

2 Floating point data output

2.1  The default output of floating point numbers

2.2  Specify the output format %m . n f

2.3  Output example

3 Case study  

3.1  Case study


1 Input of floating point data

1.1  Single precision input

(1) Format 1

Format controller :%f、%e、%g( or %F、%E、%G)
Example :float f1,f2;
scanf("%f%f",&f1,&f2); //%f for %e or %g
Such as the input : 3.14159 and 5.3, The input format is :
The correct sample 3.14159 5.3 // Default white space separation
The wrong sample :3.14159 , 5.3
(2) Format 2
Change to the following form :
scanf("f1=%f ,f2=%f ",&f1,&f2);
Enter examples correctly : f1=3.14159 ,f2=5.3

1.2  Double precision and long double precision

double Controller :%lf、%le、%lg etc. .
double d;
scanf("%lf",&d);
long double Controller :%Lf、%Le、%Lg etc. .
long double Ld;
scanf(“%Lf”,&Ld);
 

2 Floating point data output

2.1  The default output of floating point numbers

float、double Controller :%f
float f=5.6f;
double d=314.159;
printf("f=%f,d=%f",f,d); // Can not write %F
VC++6.0 in , The default output is after the decimal point 6 position . The result is :
f=5.600000,d=314.159000

2.2  Specify the output format %m . n f

Format Total seat width The fractional part Alignment mode
%m.nf m n Right
%-m.nf m n Left
%.nf There is no limit n Right
Default right alignment , rounding

2.3  Output example

float f1=3.14159f;
printf("f1=%.3f",f1);
Output results : f1=3.142
printf("f1=%.0f",f1); Indicates that only integer parts are output .
Output results : f1=3

3 Case study  

3.1  Case study

【 Case study 】 Enter the radius and height of the bottom circle of the cylinder from the keyboard , Calculate and input
Figure out the volume of the cylinder ( After decimal point 2 position ).
#include<stdio.h>
const float PI=3.14f;
int main(void)
{
	float r,h,v;
	printf("Input radius and height:\n");
	scanf("r=%f,h=%f",&r,&h);
	v=PI*r*r*h;
	printf("volume=%.2f\n",v);
	return 0;
}

 

原网站

版权声明
本文为[It's a dream, it's you!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130640014801.html