Python Forum
Convert multiple decimal numbers in string to floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert multiple decimal numbers in string to floats
#1
I'm going through the MIT Open Course Ware in my free time. Just want to begin learning, so this is not for a class or credit. Just need a nudge in the right direction, not necessarily a spelled out answer.

I'm trying to figure out how to convert a string to a float. For example:

s = "1.2, 3.4, 5.6"

I know that there is a way to do it that is probably very clever (I may not be quite as smart as I thought to take the MIT course!). Wondering if anyone else is familiar with this conundrum? Wall
Reply
#2
Hi,

Are you trying to get 3 floats out of it? 1.2 then 3.4 and finally 5.6
Or any float e.g 1.23456?
Reply
#3
First you will need to split the string on the commas. That gives you a list of strings. Then, iterate through the list converting the strings to floats.

There is a really cool Python construct called a list comprehension that works really well on the string list to float list conversion.
Reply
#4
(Aug-12-2020, 06:47 PM)Intr0spective Wrote: Hi,

Are you trying to get 3 floats out of it? 1.2 then 3.4 and finally 5.6
Or any float e.g 1.23456?

Yes to your first question, no to your second question.

(Sorry for the delay in my response!)
Thanks!
Reply
#5
so what have you tried so far? you did try something, right?
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
(Aug-15-2020, 06:37 PM)Katy8 Wrote:
(Aug-12-2020, 06:47 PM)Intr0spective Wrote: Hi,

Are you trying to get 3 floats out of it? 1.2 then 3.4 and finally 5.6
Or any float e.g 1.23456?

Yes to your first question, no to your second question.

(Sorry for the delay in my response!)
Thanks!

Katy8,

Then as jefsummers advised:

1.) split the string "1.2, 3.4, 5.6" on commas, that means you pass a ',' as a parameter to split() method. This gives you a list of strings. If you do it right then the list should look like ['1.2', '3.4', '5.6']
2.) convert each value in this string to float. To do that you have to loop/iterate over each value in the list converting it to float - you may print it or store it again in list - depends on what you plan to do with it. E.g if storing it in list then the list should look like
[1.2, 3.4, 5.6]

Lastly, if you post your code folks here will tell you what is wrong with it rather than
coding it for you Wink
Reply
#7
Thanks to both of you. I'm not sure if this is the way that the authors intended, but I may just move on and try to go back to it later.

Thanks also for the general posting tip!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,369 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,935 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Binary to decimal, print the decimal rs74 3 1,991 Jul-12-2020, 05:25 PM
Last Post: DPaul
  Adding string numbers, while loop and exit without input. Jose 11 7,371 Apr-15-2020, 08:34 AM
Last Post: Jose
  Error could not convert string to float: deadendstreet 4 5,318 Oct-02-2019, 05:49 PM
Last Post: ichabod801
  how to convert list into string Shevach 3 2,587 May-14-2019, 09:51 AM
Last Post: perfringo
  Round off floats in a list nagymusic 10 4,693 Apr-02-2019, 12:43 AM
Last Post: nagymusic
  Convert string to a specific number of bytes artblinked 1 2,399 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  def functions and floats alwillia 3 4,383 May-26-2018, 07:31 PM
Last Post: buran
  Divide a number - Multiple levels - Sum of all numbers equal to input number pythoneer 17 8,643 Apr-20-2018, 04:07 AM
Last Post: pythoneer

Forum Jump:

User Panel Messages

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