---
title: "Print float to 2 and 4 decimals"
subject: "Computer Programming"
chapter: 4
key: "semester_1/computer_programming:print-float-two-four-decimals"
source: https://www.bctengineer.com/q/semester_1/computer_programming/print-float-two-four-decimals
site: BCT Engineering
---

# Print float to 2 and 4 decimals

Computer Programming · Chapter 4: Input and Output

- Asked in: 2069 Chaitra, 2 marks
- Video: [PRINTING A FLOATING POINTS UPTO REQUIRED DECIMALS IN C](https://www.youtube.com/watch?v=aD7gVoJ2qEI) — Coder King
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/print-float-two-four-decimals
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-4

## Question

Write a program that prints the floating point value with following specification.
i) Correct up to two decimal places
ii) Correct up to four decimal places.

## Short answer

Use printf("%.2f", x) for two decimal places and printf("%.4f", x) for four decimal places, which rounds the value. For x = 3.14159, output becomes 3.14 and 3.1416 respectively.

## Answer

The precision in the `%f` specifier (`%.nf`) sets the number of digits after the decimal point; the value is rounded.

```c
#include <stdio.h>
int main(void)
{
    float x = 3.14159265f;
    printf("Two decimal places : %.2f\n", x);
    printf("Four decimal places: %.4f\n", x);
    return 0;
}
```
Output:
```text
Two decimal places : 3.14
Four decimal places: 3.1416
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which specifier prints a float with 2 decimals?
   A. %2f
   B. %.2f
   C. %f2
   D. %2.f
   Answer: B. %.nf sets n digits after the point. (Key point: Precision)

2. printf("%.4f", 3.14159265f) prints:
   A. 3.1415
   B. 3.1416
   C. 3.14
   D. 3.14159
   Answer: B. Rounded to 4 decimals gives 3.1416. (Key point: Four decimals)

3. The value printed is:
   A. truncated
   B. rounded
   C. unchanged
   D. random
   Answer: B. The value is rounded, not cut. (Key point: Rounding)

## Related in Computer Programming

- [Correct program: do-while and pow](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-sum-root) — Computer Programming, chapter 3
- [Correct program: Celsius to Fahrenheit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-celsius-fahrenheit) — Computer Programming, chapter 3
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Decimal to binary conversion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adecimal-to-binary) — Computer Programming, chapter 5
- [Binary to decimal in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-binary-to-decimal) — Computer Programming, chapter 11

## Related in other subjects

- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Floating point addition and subtraction](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Afp-add-sub) — Computer Organization and Architecture, chapter 5, asked 6×
- [Floating point division algorithm](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Afp-division) — Computer Organization and Architecture, chapter 5, asked 4×
- [Floating point multiplication](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Afp-multiplication) — Computer Organization and Architecture, chapter 5, asked 3×
