Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] a 4-digit display
#1
Hello,

I would like to reproduce a 4-digit display with floating point from 0.001 to 9999.
All the digits must be lit (no free space on the left or right)

Possible displays :
0.010 (not 0.01 or 00.01)
0.123
1.200 (not 1.2 or 01.20 or 001.2)
12.34
123.0 (not 123 or 0123)
1234 (no decimal separator)

Among the many examples of the format function on the web, I haven't found any simple solution. Do you have one ?
I look forward to reading you. Rolleyes
Best,
Reply
#2
I think you found something that nobody else wants to do. I take it that the decimal display is separate from the digits. I think this works:
def fexpand(value, width=4):
    """format value to width digits"""
    d = f"{value:.{width-1}f}".index(".")
    if d >= width:
        return f"{value:.0f}"  # Show no decimal, but round
    return f"{value:.{width-d}f}"  # Show width - d decimal digits

for value in (1, 12, 123, 1234, 1234.8, 0.1, 0.01, 0.001, 0.0001, 0.0008, 1.1, 12.12, 123.123, 1234.1234):
    print(value, "->", fexpand(value))
Pedroski55 likes this post
Reply
#3
Many thanks deanhystad Smile
Short and clean.
This displays the most acurate decimal number between 0.001 and 9999 on 4-digits:
0.010 is more acurate than 0.01
1.000 is much more acurate than 1
Several displays I use in my lab are working in this way ;-)

Merci
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 418 Jan-27-2024, 05:23 PM
Last Post: Winfried
  How to display <IPython.core.display.HTML object>? pythopen 3 46,169 May-06-2023, 08:14 AM
Last Post: pramod08728
  prefix ID Number with 0,00 make 3 digit. mg24 1 798 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  if a string has a digit - print tester_V 2 2,196 Jan-16-2021, 04:48 AM
Last Post: tester_V
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,280 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Is this right code for 3 digit pin ? fatjuicypython 1 1,853 Aug-21-2020, 11:15 PM
Last Post: bowlofred
  Writing a code with 3 digit numbers while they cant contain 0 and then sum them AFKManager 2 2,448 Jan-13-2019, 12:00 PM
Last Post: perfringo
  syntax for endswith a digit birdieman 2 5,689 Mar-04-2017, 07:21 PM
Last Post: birdieman

Forum Jump:

User Panel Messages

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