I have a variable in Python containing a floating point number (e.g. num = 24654.123
), and I\'d like to determine the number\'s precision and scale values (in t
If you need to check precision, you can try:
def prec_check(a,b)
a = str(a)
b = str(b)
do = bool(True)
n = 0
while do == True:
if a and b and a[n] == a[b]:
n += 1
else:
do = false
return n
seems like str
is better choice than repr
:
>>> r=10.2345678
>>> r
10.234567800000001
>>> repr(r)
'10.234567800000001'
>>> str(r)
'10.2345678'
(0) Please confirm or deny: You are given floats to use, this is unavoidable, you can't get your data as decimal, the Oracle datatypes include decimal-based types, and this fundamental mismatch is unavoidable. Please explain any full or partial denial.
(1) Your "fail for large numbers" remark is misleading/irrelevant/wrong -- you say that your starting point is a float, but 1234567890.0987654321 can't be represented as a float, as shown by the result of repr().
(2) Perhaps you could use the NEW repr (Python 2.7 and 3.1) which provides the minimum possible precision of repr(x) that still satisfies float(repr(x)) == x
E.g. old repr(1.1) produces "1.1000000000000001", new repr(1.1) produces "1.1"
About "I guess map(len, repr(num).split('.')) is the closest I'll get to the precision and scale of the float?": You need a strategy to handle (a) negative and zero numbers (b) numbers like 1.1e20
Digging in Objects/floatobject.c should turn up the C code for the new repr() of a float object, should you need to use Python 2.6 or earlier.
(3) Perhaps if you told us the specs for the relevant Oracle data types, we could help you devise checks for choosing which type can contain a given float value.