- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a one line C function to round floating point numbers
Here we will see how to write one-line C function, that can round floating point numbers. To solve this problem, we have to follow these steps.
- Take the number
- if the number is positive, then add 0.5
- Otherwise, subtract 0.5
- Convert the floating point value to an integer using typecasting
Example
#include <stdio.h> int my_round(float number) { return (int) (number < 0 ? number - 0.5 : number + 0.5); } int main () { printf("Rounding of (2.48): %d
", my_round(2.48)); printf("Rounding of (-5.79): %d
",my_round(-5.79)); }
Output
Rounding of (2.48): 2 Rounding of (-5.79): -6
- Related Articles
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- How to write a Python regular expression that matches floating point numbers?
- C Program to Multiply two Floating Point Numbers?
- Signed floating point numbers
- Program to find GCD of floating point numbers in C++
- Java program to multiply given floating point numbers
- Swift Program to Multiply Two Floating-Point Numbers
- Java Program to Multiply Two Floating-Point Numbers
- Kotlin Program to Multiply Two Floating-Point Numbers
- How to Multiply Two Floating-Point Numbers in Golang?
- C++ Floating Point Manipulation
- Write a function that generates one of 3 numbers according to given probabilities in C++
- Convert a floating point number to string in C
- Floating point comparison in C++
- How to round off a floating number using Python?

Advertisements