Convert float to String without rounding in Python

In this tutorial, we will learn about some methods through which we can convert a float to a string without rounding it using the Python Programming Language.

Table Of Contents

Introduction

So first what is a Float data type ?

  • Floats : Float data type in python represents the real numbers in fractional or exponential form.
  • Example :
# initialized a float number
float_example = 9.3

# type() will print the data type of var float_example
# which happens to be of class float.
print(type(float_example))

OUTPUT :

<class 'float'>

Above you can see is an example of float and what we need to do here is to change the class ‘float’ into class ‘str’ without rounding of the float. So now lets look about the different methods.

Advertisements

Convert float to string using str()

First method that we will be using is the str() function, which is a built in python function for type conversion to class str. Here in str() function we need to pass the float and it will return it as a string.

See the example below,

EXAMPLE :

# initialized a float 
float_example = 9.3

# type() will print the data type of var float_example
# which happens to be of class float.
print(type(float_example))

# converting float to string using str() method
str_example = str(float_example)

# type() will print the data type of var str_example
# which happens to be of class str
print(type(str_example))

print(str_example)

OUTPUT :

<class 'float'>
<class 'str'>
9.3

So in the example code and output above, you can see by using the str() function we have successfully converted the var float_example of class float to class str and stored it in var example_str which is of class str.

Convert float to string using string formatting

Next method that we will be using is one of the oldest method which is string formatting using the modulo % operator. See the example code below then we will be discussing about this method.

EXAMPLE :

# initialized a float 
float_example = 9.3

# type() will print the data type of var float_example 
# which happens to be of class float.
print(type(float_example))

# conversion to string from float using % operator
str_example = '%f' % float_example

# type() will print the data type of var str_example 
# which happens to be of class str
print(type(str_example))

print(str_example)

OUTPUT :

<class 'float'>
<class 'str'>
9.300000

Mathematically, modulo operator returns the remainder by dividing LHS of the modulo by RHS of the modulo. But when using with string formatting, it acts as a converter for values, here in above code %f denotes for Floating values and it denotes, in palce of %f a float value will be inserted and then the modulo operator changes it into class str. In the code and output above you can see the var str_example is of class str.

Convert float to string using repr() function

Another method through which we can convert a float to string without rounding it in Python is by using the repr() function. It takes an object as and argument and returns the printable represetational string.

See example below,

CODE :

# initialized a float number
float_example = 9.3

# type() will print the data type of var float_example 
# which happens to be of class float.
print(type(float_example))

# conversion to string from float using repr() function
str_example = repr(float_example)

# type() will print the data type of var str_example 
# which happens to be of class str
print(type(str_example))

print(str_example)

OUTPUT :

<class 'float'>
<class 'str'>
9.3

In the example code and output above, you can see by using repr() function we have successfully done the conversion of var example_float of class float to string and stored it in str_example which is of class str.

Convert float to string using format() function

Another method we can use for conversion of float to string is the format() function. Again format() function is a string formatting method which recieves two arguments :

  • variable : The variable which needs to be converted
  • foramt specifer : The format of the given variable.

It returns a formatted string. See the example code below

CODE :

# initialized a float number
float_example = 9.3

# type() will print the data type of var float_example 
# which happens to be of class float.
print(type(float_example))

# converting to string from float using format()
str_example = format(float_example,'f')

# type() will print the data type of var str_example 
# which happens to be of class str
print(type(str_example))

print(str_example)

OUTPUT :

<class 'float'>
<class 'str'>
9.300000

So in the above example, you can see by using format() method we have successfully converted given float in var example_float and to class string and stored it in example_str.

Convert float to string using Numpy

Another method that we can use is format_float_positional() function of Numpy module. Numpy is one of the most popular and widely used python module of scientific calucaltion on arrays, it is mainley used for working with arrays. Now about the format_float_positional() fucntion, It Formats a floating-point scalar as a decimal string in positional notation. Provides control over rounding, trimming and padding. Uses and assumes IEEE unbiased rounding. Uses the “Dragon4” algorithm. It recieves one parameter which is the value to format.

See the example code below,

CODE :

import numpy

# initialized a float number
float_example = 9.3

# type() will print the data type of var float_example 
# which happens to be of class float.
print(type(float_example))

# CONVERTING TO TO STRING FROM FLOAT USING THE format_float_positional()
str_example = numpy.format_float_positional(float_example)

# type() will print the data type of var str_example which 
# happens to be of class str
print(type(str_example))

print(str_example)

OUTPUT :

<class 'float'>
<class 'str'>
9.3

As you can see in the above example code and output, we have used numpy.format_float_positional() to successfully converted the given float in var float_example to class str and stored it in str_example.

Summary

So in this Python tutorial we learned about five different methods through which we can convert a float to string without rounding it in python. All the five methods are very basic and easy to understand, but by using the string formatting method you can position the converted string according to your requirement.

Also we have used Python 3.9.12 for writing example codes. Type python –version to check your python version. Always try to write example codes on your machine. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top