You have strings, and you want to end up with numbers. The numbers are not integers. Other responders have gone directly to whether you should use float or decimal as the conversion, but that is a secondary matter.

If you have integers, convert with

integer = int(number_string)

If you don't have integers, convert with

number = float(number_string)

If the number is not an integer and you need to be extremely precise with the fractional part - for example, if you need to keep exact track of exact amounts of money - you can use decimal instead of float. But in this example it seems unlikely that you need that.

The thing to be aware of that hasn't been mentioned so far is that the conversion might cause an exception if there is something wrong with one of the number strings. If you don't handle it, your program will quit running when it hits the error. That might not matter to you. If it matters, you can handle the exception in the program, but first you will need to decide what to do about such an error: should that number be omitted, should it be replaced with a placeholder, should it be replaced with float("NaN"), or what else?

In your case here, I'd suggest not handling the error until you get the basics of your program working. Then if you need to, figure out how you want to handle exceptions.


On 12/17/2022 6:51 AM, Paul St George wrote:
I have a large/long array of numbers in an external file. The numbers look like 
this:

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

When I bring the numbers into my code, they are Strings. To use the numbers in 
my code, I want to change the Strings to Float type because the code will not 
work with Strings but I do not want to change the numbers in any other way.

So, I want my Strings (above) to be these numbers.

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

Please help!











--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to