This question already has an answer here:

- Python rounding error with float numbers 2 answers
- Python weird addition bug [duplicate] 4 answers

I'm trying to add decimal numbers a decimal number and it works correctly but when I do `1.1 + 0.1`

I get `1.2000000000000002`

but all I want it to equal to is `1.2`

. When I do `1.0 + 0.1`

I get `1.1`

which is perfect but i don't get that for `1.1 + 0.1`

. So is there a way that I can get rid of the `000000000000002`

from `1.2000000000000002`

?

Thanks.

This is the literal answer to your question:

```
float(str(1.1 + 0.1)[0:3])
```

If you're interested in the "why" of the problem then refer to the links provided in the question comments.

You can try string formatting, documentation here.

```
>>> "%0.2f" % float(1.1 + 0.1)
'1.20'
```

Or Even:

```
>>> "%0.1f" % float(1.1 + 0.1)
'1.2'
```

As to why, it is explicitly described on PEP 327 here.

As has been stated countless times, 0.1 cannot be represented exactly in IEEE 754 floating point. You can read all about why in What Every Computer Scientist Should Know About Floating-Point Arithmetic or The Floating Point Guide

You can trucate or round the value:

```
>>> round(1.1+.1,2)
1.2
>>> "%.*f" % (1, 1.1+.1 )
'1.2'
>>> s=str(1.1+.1)
>>> s[0:s.find('.')+2]
'1.2'
```

If you want *exact* representation of those values, consider using the Decimal module:

```
>>> import decimal
>>> decimal.Decimal('1.1')+decimal.Decimal('.1')
Decimal('1.2')
```

Note that you need to start with the string representation of your float, `'0.1'`

since `0.1`

is not exactly representable in binary in IEEE floating point:

```
>>> decimal.Decimal(.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
```

To then get a string representation back after you calculate, you can use `str`

:

```
>>> str(sum(map(decimal.Decimal,['.1','.1','.5','.5'])))
'1.2'
```

Another alternative is to use a rational number library such as Fractions:

```
>>> from fractions import Fraction as Fr
>>> Fr(11,10)+Fr(1,10)
Fraction(6, 5)
```

With that result, you will still need to round, truncate, or use an arbitrary precision arithmetic package to get an exact number (depending on the inputs...)

Similar Questions

I am looking for a solution to calculate power of a number with a decimal exponent in XSLT 1.0. Currently the calculation method that i use calculates power only for non-decimal exponents. This is the

How do you truncate all numbers after a decimal in Python 3? For example truncate 3.444 to be just 3.

I'm trying to force Django to always store numbers with 2 decimal places but if the number is a whole one then it isn't store the .00 def clean_myfield(self): data = self.cleaned_data['myfield'] self.

My program reads a file with numbers and stores it in an array. One value, say X is 0.0000001 dataset2d = [[float(digit) for digit in line.strip().split()] for line in file] Y= dataset2d[0][2] #(fixed

recently, while working on a db2 -> oracle migration project, we came across this situation. the developers were inadvertently creating new table structures using decimal(s,p) columns. I didn't rem

exist any way to store in database a numbers with or without decimal point? The user can store to database numbers like 10 and 10.1 - for storing numbers in MySQL I use data type column decimal, so fo

Could anybody provide a regular expression for a number that has to be between 1 and 17 in length, and could optionally contain a mantissa of up to 4 places? The length of 17 includes both the charact

Possible Duplicate: C# Double - ToString() formatting with two decimal places but no rounding I'm using float numbers and I want to get the number of decimal points without any rounding-off being pe

Python Decimal doesn't support being constructed from float, it expects that you have to convert float to a string first. This is very inconvenient since standard string formattors for float require

I have regular expresion validator with expression like: ^\d{1,4}(\,\d{1,3})?$ I wan`t to validate next formats: Decimal numbers with max 3 decimal places like 0,125 1,15, 0,5 but not(1,1234) that i

Is there a fast way to convert numbers with exponential notation (examples: 0.5e10 or -5e20) to decimal or double? Update: I found C# Parse a Number from Exponential Notation but the examples won'

Im parsing xml using php and one of my variables receive a long decimal value. $ctr = 0.00529440333938; But I need to make this number as 0.52%, so I tried multiplying by 100 and forcing just 2 decim

This question already has an answer here: Matlab: How to read in numbers with a comma as decimal separator? 4 answers I've got a bunch of .dat files, where the decimal separator is comma instea

I have a string with a floating point number in it, but I can't get JSON to load it as a decimal. x = u'{14: [4.5899999999999999, susan]}' json.loads(x, parse_float = decimal.Decimal) This return

I want to to represent two numbers in A/B format,wWhere A and B are any integers. I want the decimal result in shortest format. Examples: The shortest decimal representation of 3/28 is 0.10(714285)

I know there are ways to get the number of digits after a decimal point, for instance the substring method, but how would I go about doing this for the number of digits before a decimal place? I need

Possible Duplicate: Round a double to 2 significant figures after decimal point I know that there is plant of examples how to round this kind numbers. But could someone show me how to round double,

is there a way of formatting a string to show whole numbers of a decimal number if it follows 00. example show 10 if number is 10.00. but show 10.2 if number is 10.2 this is for c#, asp.net

I have a database field defined as decimal(9,9). The edmx model in C# maps this field to a decimal variable with a precision and scale of 9. I put the number 5 in the variable. Everywhere I can check

I have a number like this 20000 and a other number like this 3. the second number is the decimal place of first number. the result must show like this 20.000 but i have no idea how i can do it. I have

I was wondering if there is a type in Numpy that allows numbers with around 20 decimal places, besides the type decimal. If not, do you have a suggestion to achieve the speed I would get performing

How do you write a number with two decimal places for sql server?

I am trying to get a fraction part of a decimal number in rails. For example I have a number, that 1.23 and I want to get 23 It is may be too easy but, does anyone have any idea about how can I do

I have a quite puzzling issue that I suspect has to do with scientific notation and decimal precision. Here is part of my code: def atan(x): # Calculate arctan(1/x) x = Decimal(x) current_value = De

I have been trying to write a Lex program to convert octal numbers to decimal numbers. But after accepting the number as input, but not doing anything. No output. And no program termination. The progr

This question already has an answer here: Rounding Number to 2 Decimal Places in C 11 answers Let me know how to round decimal for 2 decimal places in Objective-C. I would like to do like this.

How to restrict the number of digits after the decimal point using onkeyup even in javascript? I want to restrict the number of digits after the decimal point based on an input already given in the

vol = Decimal.Round(exposure.Volatility, 5); This won't let me round exposure.Volatility to a decimal b/c it say's conversion from decimal? to decimal cannot take place. How do I round that number

I have a textbox which should accept input in the following format: [decimal], [decimal]; [decimal], [decimal]; [decimal], [decimal] For example: 0.01,0.10; 0.20,0.50; 1.00,3.00; 4.00,1000.00 My know

I am making a calculator and I want to make sure that user is able to enter a Negative Decimal Number I see the option in the xml file separately for example in order to put the Decimal Number, the

I have a decimal number like this: 62.000,0000000 I need to cast that decimal into int; it will always have zero decimal numbers; so I wont loose any precision. What I want is this: 62.000 Stored on a

How can I add, subtract, and compare binary numbers in Python without converting to decimal?

I want to truncate numbers (given as strings) to a fixed number of decimal places. The numbers can be negative (with a minus sign), positive (no sign). I'd prefer to round the numbers properly and kee

This question already has an answer here: how can I force division to be floating point in Python? 7 answers When I do a division in Python/Pandas (e.g. 47/100) how do I show the decimal value

I have a float: 1.2333333 How do I change it into a two decimal number with a comma as a decimal point separator, eg 1,23?

I am trying to get the total of my variables containing numbers, some may be decimals. I need this to be to two decimal places and am using the number_format() function. $total = $order->order->

I've been looking for formatting decimal numbers easily as in JavaScript by using toFixed(). What's your suggestion?

I am working on a code and there I get data in the form of : k=[{'item_id': '43381702', 'length': {'_exp': -2, '_is_special': False, '_int': '0', '_sign': 0}, 'height': {'_exp': -2, '_is_special': Fa

This is my Code so far. I want to know how to return the answer with two decimal places. EX. Instead of 515.3 I want 515.30 or instead of 500 I want it to return 500.00. <HTML> <HEAD> <

i have an algorithym that gives back a number with a decimal point (I.E. .57). What I would like to do is just get the 57 without the decimal. I have used eregi_replace and str_replace and neither

I was wondering how to make avg in the following program round to 3 decimal places: public class BaseballCalculator { public static void main(String args[]) { Scanner myScanner = new Scanner(System.i

How do I get only the numbers after the decimal? Example: 2.938 = 938

I'm fairly new to Python, and was wondering how would I be able to control the decimal precision of any given number without using any the decimal module or floating points (eg: %4f %n). Examples (

In Python 2.6.5 the following expression yields False: >>> import decimal >>> decimal.Decimal('0') < 1.0 False Is there a rationale explaining why comparison of Decimal against f

In my program I am printing float numbers to file. There is high precision of these numbers so there are many digits after decimal point, i.e number 0.0433896882981. How can I reduce number of digits

I have following jQuery function to make textbox accepting only decimal number, but problem is it didn't accept . , so it works as only integer textbox, I don't want to use any plugin here is the jQue

Possible Duplicate: How do I change the number of decimal places iOS? If using Xcode to make an iOS calculator, how would I add a decimal button? I am looking for an iOS tutorial on how to make a (s

Whenever I insert a value like: 1 into a decimal field it will immediately rejects it. Is there any code in PHP to convert a value of say 1 to 01.00 or 1.00

first time I post to this round, so please bear with me if I don't follow all the rules properly. I am writing an app for the iPhone (OS 3.1) and am trying to write some code which lets me add decimal

Possible Duplicate: Gray code in .NET I want to get Gray code of a number from its Decimal equivalent. Example: Dec Gray Binary 0 000 000 1 001 001 2 011 010 3 010 011 4 110 100 5 111 101 6 101 110