How do I shift the decimal place in Python?
Question:
I’m currently using the following to compute the difference in two times. The out – in is very fast and thus I do not need to display the hour and minutes which are just 0.00 anyway. How do I actually shift the decimal place in Python?
def time_deltas(infile):
entries = (line.split() for line in open(INFILE, "r"))
ts = {}
for e in entries:
if " ".join(e[2:5]) == "OuchMsg out: [O]":
ts[e[8]] = e[0]
elif " ".join(e[2:5]) == "OuchMsg in: [A]":
in_ts, ref_id = e[0], e[7]
out_ts = ts.pop(ref_id, None)
yield (float(out_ts),ref_id[1:-1], "%.10f"%(float(in_ts) - float(out_ts)))
INFILE = 'C:/Users/kdalton/Documents/Minifile.txt'
print list(time_deltas(INFILE))
Answers:
The same way you do in math
a = 0.01;
a *= 10; // shifts decimal place right
a /= 10.; // shifts decimal place left
or use the datetime module
>>> import datetime
>>> a = datetime.datetime.strptime("30 Nov 11 0.00.00", "%d %b %y %H.%M.%S")
>>> b = datetime.datetime.strptime("2 Dec 11 0.00.00", "%d %b %y %H.%M.%S")
>>> a - b
datetime.timedelta(-2)
Expanding upon the accepted answer; here is a function that will move the decimal place of whatever number you give it. In the case where the decimal place argument is 0, the original number is returned. A float type is always returned for consistency.
*Edit: Fixed cases of inaccuracies due to floating-point representation.
def moveDecimalPoint(num, places):
'''Move the decimal place in a given number.
Parameters:
num (int/float): The number in which you are modifying.
places (int): The number of decimal places to move.
Returns:
(float)
Example:
moveDecimalPoint(11.05, -2) #returns: 0.1105
'''
from decimal import Decimal
num_decimal = Decimal(str(num)) # Convert the input number to a Decimal object
scaling_factor = Decimal(10 ** places) # Create a scaling factor as a Decimal object
result = num_decimal * scaling_factor # Perform the operation using Decimal objects
return float(result) # Convert the result back to a float
def move_point(number, shift, base=10):
"""
>>> move_point(1,2)
100
>>> move_point(1,-2)
0.01
>>> move_point(1,2,2)
4
>>> move_point(1,-2,2)
0.25
"""
return number * base**shift
I’m currently using the following to compute the difference in two times. The out – in is very fast and thus I do not need to display the hour and minutes which are just 0.00 anyway. How do I actually shift the decimal place in Python?
def time_deltas(infile):
entries = (line.split() for line in open(INFILE, "r"))
ts = {}
for e in entries:
if " ".join(e[2:5]) == "OuchMsg out: [O]":
ts[e[8]] = e[0]
elif " ".join(e[2:5]) == "OuchMsg in: [A]":
in_ts, ref_id = e[0], e[7]
out_ts = ts.pop(ref_id, None)
yield (float(out_ts),ref_id[1:-1], "%.10f"%(float(in_ts) - float(out_ts)))
INFILE = 'C:/Users/kdalton/Documents/Minifile.txt'
print list(time_deltas(INFILE))
The same way you do in math
a = 0.01;
a *= 10; // shifts decimal place right
a /= 10.; // shifts decimal place left
or use the datetime module
>>> import datetime
>>> a = datetime.datetime.strptime("30 Nov 11 0.00.00", "%d %b %y %H.%M.%S")
>>> b = datetime.datetime.strptime("2 Dec 11 0.00.00", "%d %b %y %H.%M.%S")
>>> a - b
datetime.timedelta(-2)
Expanding upon the accepted answer; here is a function that will move the decimal place of whatever number you give it. In the case where the decimal place argument is 0, the original number is returned. A float type is always returned for consistency.
*Edit: Fixed cases of inaccuracies due to floating-point representation.
def moveDecimalPoint(num, places):
'''Move the decimal place in a given number.
Parameters:
num (int/float): The number in which you are modifying.
places (int): The number of decimal places to move.
Returns:
(float)
Example:
moveDecimalPoint(11.05, -2) #returns: 0.1105
'''
from decimal import Decimal
num_decimal = Decimal(str(num)) # Convert the input number to a Decimal object
scaling_factor = Decimal(10 ** places) # Create a scaling factor as a Decimal object
result = num_decimal * scaling_factor # Perform the operation using Decimal objects
return float(result) # Convert the result back to a float
def move_point(number, shift, base=10):
"""
>>> move_point(1,2)
100
>>> move_point(1,-2)
0.01
>>> move_point(1,2,2)
4
>>> move_point(1,-2,2)
0.25
"""
return number * base**shift