Python Forum

Full Version: Adding a comma in the resulting value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good day. I receive data from the device in the form: [1663] but it should be written as 166.3. Tell me how to do this?
data_from_the_device = [1663]
written_as = data_from_the_device[0] * 0.1
print(written_as)
Output:
166.3
Is the data a number, a string, a bytearray, what?
(May-15-2021, 10:29 PM)Yoriz Wrote: [ -> ]
data_from_the_device = [1663]
written_as = data_from_the_device[0] * 0.1
print(written_as)
Output:
166.3
It worked, but some values ​​are of the form:
166.3
9.4
21.900000000000002
181.0
47.800000000000004
12.200000000000001
use string formatting
print(f'{written_as:.1f}')
or

data_from_the_device = [1663]
print(f'{data_from_the_device[0] * 0.1:.1f}')
(May-16-2021, 04:42 AM)buran Wrote: [ -> ]use string formatting
print(f'{written_as:.1f}')
or

data_from_the_device = [1663]
print(f'{data_from_the_device[0] * 0.1:.1f}')

Everything worked, thank you so much
There was another problem. Now I want to write this data to the database. But how to write correctly:

Now I write like this:

 cursor.execute(sql, (newHireDate, result1[0] * 0.1, result2[0] * 0.1,result3[0] * 0.1,result4[0] * 0.1,result5[0] * 0.1,result6[0] * 0.1,))
But in the database it is written as:
166.3
9.4
21.900000000000002
181.0
47.800000000000004
12.200000000000001