Python Forum
Adding a comma in the resulting value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a comma in the resulting value
#1
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?
Reply
#2
data_from_the_device = [1663]
written_as = data_from_the_device[0] * 0.1
print(written_as)
Output:
166.3
stsxbel likes this post
Reply
#3
Is the data a number, a string, a bytearray, what?
stsxbel likes this post
Reply
#4
(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
Reply
#5
use string formatting
print(f'{written_as:.1f}')
or

data_from_the_device = [1663]
print(f'{data_from_the_device[0] * 0.1:.1f}')
stsxbel likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix With n_samples=0, test_size=0.2 and train_size=None, the resulting train s MrSonoa 2 2,888 Apr-15-2023, 12:02 PM
Last Post: MrSonoa
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,112 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 1,360 Jun-13-2022, 10:11 PM
Last Post: dee
  How to store the resulting Doc objects into a list named A xinyulon 1 1,893 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  decimal comma DPaul 9 2,268 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  How to instantly add quotation marks and comma for parameters? cheers100 4 8,049 Oct-22-2020, 12:51 PM
Last Post: cheers100
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,795 Sep-02-2020, 03:32 AM
Last Post: flour_power_33
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 4,007 Apr-10-2020, 02:39 PM
Last Post: buran
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 2,653 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck
  Adding markers to Folium map only adding last element. tantony 0 2,121 Oct-16-2019, 03:28 PM
Last Post: tantony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020