Python Forum
replacement for fpformat.extract
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replacement for fpformat.extract
#1
I have some python 2.7 code that I need to update to 3.10. The code used fpformat.extract() to determine the following:

Python 2.7 Code
  floatLst=[-10999999.0,-10234.0,-123.456,-10.4,-0.00137,0.0,0.00137,10.4,123.456,10234.0,10999999]
  fmt = "%.8E"
  for numFloat in floatLst:
    sign, intPart, fractPart, exponent = fpformat.extract(fmt % numFloat)
    print (sign,intPart,fractPart,exponent)
The closet equivalent that I've found so far in Python 3.10 uses Decimal.

  from decimal import *

  floatLst=[-10999999.0,-10234.0,-123.456,-10.4,-0.00137,0.0,0.00137,10.4,123.456,10234.0,10999999]
  for numFloat in floatLst:
    sign,digits,exponent = Decimal(numFloat).as_tuple()
    print (numFloat,digits,exponent,len(digits))
This definitely does not work as well. Due to rounding both digits and exponent can be confusing.
Is there another class, or methods of either floats or strings that is specifically designed to replace fpformat.extract()?
I have search quite extensively for solutions to this question.

Any guidance will be greatly appreciated.
Gribouillis write Apr-12-2024, 02:43 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
replacement for fpformat.extract - by GbSig1998 - Apr-12-2024, 01:41 PM
RE: replacement for fpformat.extract - by GbSig1998 - Apr-12-2024, 04:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String replacement in DB WJSwan 0 789 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,833 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Extract the largest value from a group without replacement (beginner) preliator 1 2,135 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,163 Jun-30-2020, 06:38 PM
Last Post: asaxty
  line replacement help mdalireza 8 3,599 Nov-11-2019, 12:54 PM
Last Post: mdalireza
  xml replacement with python josesalazmit 3 8,483 Feb-24-2019, 07:28 PM
Last Post: stullis
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 2,540 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  Is pathlib a viable replacement for os.path? j.crater 4 10,112 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Using python for text replacement omar 1 3,902 Dec-22-2016, 01:36 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020