×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

Input float value and print it with specified digit after decimal point in C

Here, we are going to learn how to input a float value and print the value with specified given number of digits after the decimal point in C programming language?
By IncludeHelp Last updated : March 10, 2024

Problem statement

Input a float value and we have to print the input value by specifying/setting different decimal precision in C.

Example

Input:
Enter float value: 12.34567

Output:
12
12.3
12.35
12.3457

Setting decimal precision

To set decimal precision, we use the following format specifier with the printf() statement,

  • %.0f : No digit after decimal point
  • %.2f : 2 digits after decimal point
  • %.4f : 4 digits after decimal point

Program

# include <stdio.h>

int main ()
{
	float value;	

	printf("Enter float value: ");
	scanf("%f", &value);

	//print without decimal point 
	printf("%0.0f\n", value);
	//print 1 digits after decimal point
	printf("%0.1f\n", value) ;
	//print 2 digits after decimal point 
	printf("%0.2f\n", value) ;
	//print 4 digits after decimal point 
	printf("%0.4f\n", value);

	return 0;
}

Output

Enter float value: 1.234567
1
1.2
1.23
1.2346

C scanf() Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.