First of all, thanks for the attention, I am new to this site ^^ Therefore, forgive me while doing something wrong .. .
I have a big problem with my Python code ... I am new to programming, and I am also new to Python.
I need to take a floating point number and move the point right, so it becomes an integer, like getting 60.27 and 6027.
The algorithm I am using is repeatedly num * 10
num% 2 == 0 , then
int (num) Getting is happening.
The problem is, when I multiply (for example)
602.47 * 10 it returns
6024.700000000001 and it does not clearly work Is: -)
Is there any way to fix this, or any other technique or other way is it recursive ?? I am allowed to use anything that I want but it should be recursive:
for
or
while ...
Thanks for the help! ! My first language is not English, so I apologize if it is difficult to read ...
You can try something:
x = 60.27 newx = int (str (x) .replace ('.', '')) Edit: As a side note, string . Replace and .translate have similar performance for floats of different sizes
% Timeit int (str (4.73285) .replace ('.', ')) 100000 loops, best 3: 2.65 us per loop% timeit int (str (4.73285). Translestate (none,'. ')) 100000 loops, best 3: 3.02 us loop
Comments
Post a Comment