Hello,
I have a list of tuple with strin elements. These elements are number,
but they are save as string. Now I will change the string to number
which will be rounded. An example will make it more clear.
t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]
And I will have the next list:
t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]
The elements of tuple are not more as string.
Would somebody tell me how I can do that?
Regards,
Nader 3 2518
Nader wrote:
Hello,
I have a list of tuple with strin elements. These elements are number,
but they are save as string. Now I will change the string to number
which will be rounded. An example will make it more clear.
t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]
And I will have the next list:
t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]
The elements of tuple are not more as string.
Would somebody tell me how I can do that?
use
float("123.45")
to convert a string to a float.
Of course you need to do that on all your elements above by e.g. a
list-comprehension.
Diez
On Jun 10, 4:30 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Nader wrote:
Hello,
I have a list of tuple with strin elements. These elements are number,
but they are save as string. Now I will change the string to number
which will be rounded. An example will make it more clear.
t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]
And I will have the next list:
t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]
The elements of tuple are not more as string.
Would somebody tell me how I can do that?
use
float("123.45")
to convert a string to a float.
Of course you need to do that on all your elements above by e.g. a
list-comprehension.
Diez
If I do the next :
t1 = [(round(float(x),1), round(float(y),2)) for x, y in t]
I get the long float as :
[(35.799999999999997, -0.23999999999999999), (33.299999999999997,
-2.71), (33.600000000000001,-2.4199999999999999)]
But I would have a float with 2 decimal numbers.
>
If I do the next :
t1 = [(round(float(x),1), round(float(y),2)) for x, y in t]
I get the long float as :
[(35.799999999999997, -0.23999999999999999), (33.299999999999997,
-2.71), (33.600000000000001,-2.4199999999999999)]
But I would have a float with 2 decimal numbers.
There is no such thing as a float with only two decimal numbers. This has
been discussed on this ML a bazillion times - what you see above are
rounding errors due to the approximation of decimal values by binary
floating points.
You can *convert a float to a string* and specify a precision when printing:
>>print "%.2f" % 2.4199999999999999999999999
2.42
Or you can use the module decimal to work with numbers base 10 - which you
can also limit to certain precisions. See a recent thread on this ML,
google for "Alternative to decimal type"
Diez This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bengt Richter |
last post by:
Peculiar boundary cases:
>>> 2.0**31-1.0
2147483647.0
>>> int(2147483647.0)
2147483647L
>>> int(2147483647L )
2147483647
>>>
>>> -2.0**31
|
by: Benny |
last post by:
Hi,
Just wonder why
PRINT CAST(0.0573542567654 AS float)
will give the rounded reult
0.0573543
|
by: V. Jenks |
last post by:
I'm having a problem with the Convert.ToSingle and the
O:C formatting rounding off my floating-point numbers.
When I .ToString a float, it rounds the number. It also
rounds it if I use the "C"...
|
by: karthi |
last post by:
hi,
I need user defined function that converts string to float in c.
since the library function atof and strtod occupies large space in
my processor memory I can't use it in my code.
regards,...
|
by: Erick-> |
last post by:
hi all...
I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??
thanks
Erick
|
by: Dilip |
last post by:
Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused...
|
by: mygolfcircle |
last post by:
Hi everyone,
I'd like some help to round UP a float value to the nearest higher integer.
This means,
1.2 will be rounded up to 2
2.15 will be rounded up to 3
4.01 will be rounded up to 5...
|
by: Dom Fulton |
last post by:
Has anyone got a mechanism for finding the number of bits in floats,
doubles, and long doubles? I need this to communicate with some
hardware.
I guess I could try to deduce this from float.h,...
|
by: DWilli3501 |
last post by:
I have two short programs that use floating point numbers. The first gives an incorrect result. By simply placing a "cout" (or any executable line) in the correct location of the program I get a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |