Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89844

Re: Converting 5.223701009526849e-05 to 5e-05

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder03.ams.xsnews.nl!abp002.ams.xsnews.nl!frontend-F10-20.ams.news.kpn.nl
From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Converting 5.223701009526849e-05 to 5e-05
Organization Decebal Computing
References <87vbgakrlr.fsf@Equus.decebal.nl> <mailman.52.1430642455.12865.python-list@python.org>
X-Face "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E
X-Homepage http://www.decebal.nl/
Date Sun, 03 May 2015 11:22:18 +0200
Message-ID <87mw1mkoc5.fsf@Equus.decebal.nl> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:12CTlAkXhc7SRplJIbwzJUYoOaw=
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Lines 56
NNTP-Posting-Host 81.207.62.244
X-Trace 1430645329 news.kpn.nl 21163 81.207.62.244@kpn/81.207.62.244:35465
Xref csiph.com comp.lang.python:89844

Show key headers only | View raw


Op Sunday 3 May 2015 10:40 CEST schreef Ben Finney:

> Cecil Westerhof <Cecil@decebal.nl> writes:
>
>> When I have a value like 5.223701009526849e-05 in most cases I am
>> not interested in all the digest after the dot.
>
> What type of value is it?

If the absolute value is bigger as 0 and smaller as 1, it should be a
float. ;-)


> A ‘float’ value has many different textual representations, most of
> them inaccurate. So talking about the digits of a ‘float’ value is
> only partly meaningful; digits are a property of some chosen
> representation, not intrinsic to the number.
>
> A ‘str’ value can be converted in various ways, but is useless as a
> number until you create a new number from the result.
>
> Choosing a solution will rely on understanding that the textual
> representation of a number is not itself a number; and vice versa, a
> number value does not have a canonical text representation.

It is because I display things like:
    02:47:18: Increase memoize -> iterative 19
              (0.0004942629893776029 / 2.475001383572817e-05)

And that is way to specific.


>> Is there a simple way to convert it to a string like '5e-05'?
>
> Assuming we're talking about a ‘float’ value::
>
>>>> foo = 5.223701009526849e-05
>>>> "{foo:5.1}".format(foo=foo)
> '5e-05'
>
> See the ‘str.format’ documentation, especially the detailed
> documentation for the “format specification mini-language”
> <URL:https://docs.python.org/3/library/string.html#format-specification-mini-language>
> for how to specify exactly how you want values to be formatted as
> text.

Very interesting documentation. I go for:
    '{foo:.3E}'.format(foo=foo)

Then it simplifies also big numbers and it works for int's also. (Not
needed now, but it never hurts to be prepared for the future.)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Converting 5.223701009526849e-05 to 5e-05 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 10:11 +0200
  Re: Converting 5.223701009526849e-05 to 5e-05 Ben Finney <ben+python@benfinney.id.au> - 2015-05-03 18:40 +1000
    Re: Converting 5.223701009526849e-05 to 5e-05 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 11:22 +0200
      Re: Converting 5.223701009526849e-05 to 5e-05 Chris Angelico <rosuav@gmail.com> - 2015-05-03 19:51 +1000
        Re: Converting 5.223701009526849e-05 to 5e-05 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 12:02 +0200
          Re: Converting 5.223701009526849e-05 to 5e-05 Chris Angelico <rosuav@gmail.com> - 2015-05-03 20:57 +1000
      Re: Converting 5.223701009526849e-05 to 5e-05 Dave Angel <davea@davea.name> - 2015-05-03 07:34 -0400
  Re: Converting 5.223701009526849e-05 to 5e-05 Ben Finney <ben+python@benfinney.id.au> - 2015-05-03 18:48 +1000
    Re: Converting 5.223701009526849e-05 to 5e-05 Alexander Blinne <news@blinne.net> - 2015-05-07 10:00 +0200

csiph-web