How To Round In Python Without Using The Round Function? -my Programming school

Many people have questioned how we can round a number in python without using the round function. To round the number without using the round function we will do it without math just using string and the syntax is “%0.1f” % number_to_round. It’s easy to do but there is a different way that we can round numbers without using the round function and that is some complect but we will try to make it easy and explain it in deep.

In the article, we discuss this in detail so you will never need to visit other sites, and if you want to learn a complete tutorial about Python programming so you can go through the Python Tutorial list.

What is Round Function?

Before starting coding and writing code without using a round function, let us understand first what is round function. In Python, there are many functions available that help to reduce the programmer’s effort. It helps to reduce the number of code and increase performance. 

In the python programming language round is a function that helps to return the round number of the floating number. If number_digits is greater than 0 the number is rounded to the specified number of decimal places. 

Let’s see for example,

n = 12.8376

print(round(n))

Output: 13

How to round off numbers in Python without using round function?

There are different ways to round the number in python without using the round function. In this article we will mostly talk two ways:

Example1: To round a number without a round function

Syntax:

int((number*10)+0.5)/10.0 or "%0.1f" % number_to_round

Rounding all number

n = 12.8376

x = int(n-0.5)+1

print('After rounded the number is:',x)

Output:

After rounded the number is: 13

What to use instead of round function?

In some articles, you will see CEIL(), FLOOR(), and TRUNC() are the same but the use of all functions is somehow similar but not exactly the same. Let’s here we will discuss what is Ceil() function, Floor() Function, and Trunc() function with examples.

Ceil() Function:

The ceil() function prints the ceiling number of the value of x i.e, It will return the greater or equal value of x. Example,

impot math

print("math.ceil(300.11) : ", math.ceil(300.11))

print("math.ceil(300.00) : ", math.ceil(300.00))

Output:

math.ceil(300.11) :  301

math.ceil(300.00) :  300

Floor() Function:

The floor() function prints the floor value of a given number. It will round a number down to the nearest integer. If x = 300.96, 300.11,  300.11, 0.12, -5.12, or -5.0 it will return the nearest integer number. Example:

import math

print("math.floor(300.72) : ", math.floor(300.96))

print("math.floor(300.72) : ", math.floor(300.11))

print("math.floor(300.72) : ", math.floor(300.00))

print("math.floor(0.12) : ", math.floor(0.12))

print("math.floor(-5.12) : ", math.floor(-5.12))

print("math.floor(-5.0) : ", math.floor(-5.0))

Output:

math.floor(300.72) :  300

math.floor(300.72) :  300

math.floor(300.72) :  300

math.floor(0.12) :  0

math.floor(-5.12) :  -6

math.floor(-5.0) :  -5

Trunc() Function:

The trunc() function returns the truncated number of the given number. It will remove the decimal number and return the integer part of a different number. Example,

import math

print("math.trunc(11.78) : ", math.trunc(11.78))

print("math.trunc(12.23) : ", math.trunc(12.23))

print("math.trunc(-99.88) : ", math.trunc(-99.88))

Output:

math.trunc(11.78) :  11

math.trunc(12.23) :  12

math.trunc(-99.88) :  -99

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment