How do I get decimals values without rounding them off

Swayam Shah

How do I add two numbers and retain their decimal places in the final answer? Below is the code to do this but it does not work.

import re
cnt=0
s=0
l1= []
with open('C://Users/S/Documents/ok5.txt') as f:
    for i in f:
        if i.startswith('X-DSPAM-Confidence:'):
            x = re.findall('\d*?\.\d+',i)
            l1.append(x)
            cnt+=1

for i in range(0,len(l1)):
    j = float(i)
    s += j

print(l1)    
print(s)

The output I get is:

[['0.5454'], ['0.5677']]
1.0

But, when I try the simple code below it gives the right answer:

a = 0.5454
b = 0.5677
c = float(a+b)
print(c)

The output for this is:

1.1131
jbflow

I think you want to do this instead:

import re
cnt=0
s=0
l1= []
with open('C://Users/S/Documents/ok5.txt') as f:
    for i in f:
        if i.startswith('X-DSPAM-Confidence:'):
            x = re.findall('\d*?\.\d+',i)
            l1.append(x)
            s += float(x[0]) # You could just add it here, which improves time complexity
            cnt+=1
print(l1)    
print(s)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get 2 place decimal without rounding off in MySQL?

How do I cut off a decimal without rounding?

How can I get the whole number without rounding it off?

How do I prevent decimals from rounding off when printed in C++?

Rounding off decimals in datatables

how to remove trailing decimals without rounding up?

How do I get rid of php decimals

How do I get this mathematical rounding?

How do I stop Excel from rounding numbers when format has been set to number and to 2 decimals?

How to cut off decimal in Java WITHOUT rounding?

How to retain 2 decimals without rounding in python/pandas?

499.28999999999996 -> 499.28 How do I remove a decimal point without rounding it?

How do I format double input in Java WITHOUT rounding it?

How can I get only the first decimal of a number but without rounding it

How can I turn off rounding in Spark?

How do I select from a given set of values, rounding up?

Once animated images are off the screen, how do I delete them?

How do I get matching values in PIG without using UDF?

How do I get this program to allow decimals to be entered?

How do I query UUID values without having to explicitly cast them to string in HTSQL

Where do values from a config or application:set_env go? How do I get them?

How do I get xargs to show me the command lines it's generating without running them?

How do I get tmux to open up a set of panes without manually entering them?

R: Plots of subset still include excluded attributes, how do I get draw a plot without them?

How to keep original decimal numbers without rounding off

Pandas Dataframe How to cut off float decimal points without rounding?

How to restrict the decimal places in awk without rounding off

How do I get a comma separated list of ten values and not all of them?

In MVC/Razor, how do I get the values of multiple checkboxes and pass them all to the controller?