0

How to convert float value to another float value with some specified precision using python?

>>> a = 0.1

but actually value of a is

0.10000000000000001

But I need to convert a to 0.1 in float type.

rnevius
  • 26,578
  • 10
  • 58
  • 86
Madhu Billy
  • 197
  • 1
  • 10

3 Answers3

2

It is not possible to have 0.1 exactly when using the float type, due to how numbers are stored internally. This offsite resources will hopefully explain the internals more easily.

Note that when you use the Python console it may look like you have 0.1 exactly but this is not true, as the code below shows

In [35]: a = 0.1

In [36]: print(a)
0.1 # Looks fine! But wait, let's print with 30 decimal places...

In [37]: print('{:.30f}'.format(a))
0.100000000000000005551115123126 # Uh oh.

This occurs because when printing in the console, only a certain number of decimal places are printed, and for 0.1 this number is such that where it starts to deviate from 0 is outside the range.

Python does have a Decimal package which can provide support for decimal numbers, as below

import decimal

a = decimal.Decimal('0.1')

print(a)
# 0.1

print('{:.30f}'.format(a))
# 0.100000000000000000000000000000

Note that I've constructed the Decimal object using the string '0.1' as opposed to the float 0.1. This is because if I had used the float then the Decimal object would have contained the same "errors" that the float has.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • For me print is not an issue. for example 142*0.1 the actual value is 14.200000000000001 but i need to truncate and save it as 14.20000000 – Madhu Billy Nov 20 '14 at 10:07
  • @Madhu I was using the printing as an example for others in the future as well as you. When it boils down to it though, **this is a limitation of the `float` type and there's nothing you can really do about it other than using a different type.** – Ffisegydd Nov 20 '14 at 10:09
0

How about the round function: Round

Rik Verbeek
  • 502
  • 3
  • 8
0

You can use formate function with float and round function also

>>> a = float(format(0.10000000000000001, '.1f'))
>>> a
0.1
>>> round(0.10000000000000001,2)
0.1
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
  • This doesn't work at all. You are simply hiding the issue by only printing to 1 decimal place. The number is still 0.10000...0001 etc. – Ffisegydd Nov 20 '14 at 09:53
  • @Ffisegydd correct me if i'm wrong but where is no float point value `0.1`. – Darth Kotik Nov 20 '14 at 09:57
  • @Darth exactly. It cannot be represented using the `float` type. If you do `print(0.1)` in the Python console then it will return `0.1` *only because Python doesn't print all the decimal places.* The number is still `0.100000000000000005551115123126...` in reality. – Ffisegydd Nov 20 '14 at 09:59
  • @Ffisegydd So issue is representing of numbers in computer memory and we can't do anything but hide to it and it's ok. – Darth Kotik Nov 20 '14 at 10:06
  • @Darth or in Python you can use `decimal` as I've explained in my answer. – Ffisegydd Nov 20 '14 at 10:06
  • don't think about printing in a console for Example 142*0.1 the actual python calculated value is 14.200000000000001 but actually i need to truncate it to 14.20000000 and i need to comapare this float value with other float value – Madhu Billy Nov 20 '14 at 10:11
  • @MadhuBilly if you need to compare values then it works `>>> float(format(142*0.1, '.2f')) ==14.20000000` True. – Vishnu Upadhyay Nov 20 '14 at 10:14