Jason Swails wrote:
>> s = ('%%%ig' % sigfigs) % n # double-% cancels the %
Thanks! I see that the parenthesis can be dropped, too:
>>> '%%.%ig' % 3 % 4.23456e-5
'4.23e-05'
/c
--
http://mail.python.org/mailman/listinfo/python-list
On Thu, Oct 7, 2010 at 11:51 AM, C or L Smith wrote:
> I just sent a similar suggestion to tutor: check out the %g format.
>
> >>> print '%g' % 1.2345e7
> 1.2345e+07
> >>> print '%g' % 1.2345e-7
> 1.2345e-07
> >>> print '%g' % 1.2345
> 1.2345
>
> >>> def me(n, sigfigs = 4):
> ... s = ('%.'+'%ig'
I just sent a similar suggestion to tutor: check out the %g format.
>>> print '%g' % 1.2345e7
1.2345e+07
>>> print '%g' % 1.2345e-7
1.2345e-07
>>> print '%g' % 1.2345
1.2345
>>> def me(n, sigfigs = 4):
... s = ('%.'+'%ig' % sigfigs) % n # check docs for a better way?
... if 'e' in s: m, e = s.s
On 2:59 PM, Steven D'Aprano wrote:
I want the mantissa and decimal exponent of a float, in base 10:
mantissa and exponent of 1.2345e7
=> (1.2345, 7)
(0.12345, 8) would also be acceptable.
The math module has a frexp() function, but it produces a base-2 exponent:
math.frexp(1.2345e7)
(0.7
On Wed, Oct 6, 2010 at 6:54 PM, Steven D'Aprano <
steve-remove-t...@cybersource.com.au> wrote:
> I want the mantissa and decimal exponent of a float, in base 10:
>
> mantissa and exponent of 1.2345e7
>
Perhaps not the prettiest, but you can always use string manipulations:
def frexp_10(decimal):
On 10/6/10 5:54 PM, Steven D'Aprano wrote:
I want the mantissa and decimal exponent of a float, in base 10:
mantissa and exponent of 1.2345e7
=> (1.2345, 7)
(0.12345, 8) would also be acceptable.
The math module has a frexp() function, but it produces a base-2 exponent:
math.frexp(1.2345e7
Steven D'Aprano wrote:
> I want the mantissa and decimal exponent of a float, in base 10:
>
> mantissa and exponent of 1.2345e7
> => (1.2345, 7)
>
> (0.12345, 8) would also be acceptable.
[...]
> Have I missed a built-in or math function somewhere?
The integral, decimal exponent is just the floo