Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python: formatting a messy string

  • 08-05-2021 10:29am
    #1
    Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭


    I have this 3d printer script I'm editing to make it a little more useful, I am not finding it useful at the minute as you have to stare at a scrolling output for up to 30 seconds or more (depending on filename length) to get the information without the script being edited. The script does have an option to disable the horizontal scrolling but I plan to (eventually) periodically scroll and show a time remaining value every so often, as well switching to the filename at intervals which will require the scrolling.

    I want to format a number as a string to 1 decimal place or maybe a whole number but it is currently printing out as long. I know int() takes 2 arguments (number, base) but I do not think it will work in this case. I'm also not too sure if format() or .format can be used with the concatenation.
    max_layer = "68" #starts out as an int and is converted to string and int in multiple places 
    current_layer = i # i == loop
    
    # "Layer " + i + " of " + max_layer
    # display_text == "Layer 18 of 68"         
    
    
    display_text += " " + "(" + str( (current_layer / int(max_layer) )  * 100 ) + "%)"
    
    print(display_text)    #for this example
    
    output:
    Layer 18 of 68 (26.47058823529412%)
    

    I'd like it to output as "(26%)" or rounded to 1 decimal "(26.5%)" depending on UX. What would be the best way to format inside the concatenated string. Given that print() will not be available in the script


Comments

  • Registered Users, Registered Users 2 Posts: 6,236 ✭✭✭Idleater


    Why not make the value numeric. That's best for computation, and can be printed as a variable in the string.


  • Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭iLikeWaffles


    Idleater wrote: »
    Why not make the value numeric. That's best for computation, and can be printed as a variable in the string.

    Because it will require a rewrite the script is already written it just needs formatting.


  • Closed Accounts Posts: 31 Bus Wanker


    Replace display text with the below line and it should do what you need.

    display_text = 'Layer 18 of 68 ({:.1f}%)'.format((current_layer / int(max_layer)*100))

    And to make it scalable using the i variable you have

    display_text = 'Layer {} of 68 ({:.1f}%)'.format(i, (i / int(max_layer)*100))


  • Registered Users, Registered Users 2 Posts: 6,236 ✭✭✭Idleater


    Because it will require a rewrite the script is already written it just needs formatting.

    Ok, well, you just need to truncate it with int(my_number)


  • Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭iLikeWaffles


    Bus Wanker wrote: »
    Replace display text with the below line and it should do what you need.

    display_text = 'Layer 18 of 68 ({:.1f}%)'.format((current_layer / int(max_layer)*100))

    And to make it scalable using the i variable you have

    display_text = 'Layer {} of 68 ({:.1f}%)'.format(i, (i / int(max_layer)*100))

    Perfect, that's the one! Thank you.
    display_text += " " + "(" + "{:.0f}".format((current_layer / int(max_layer) )  * 100 ) + "%)"
    
    #or
    
    display_text += " " + "(" + "{:.1f}".format((current_layer / int(max_layer) )  * 100 ) + "%)"
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭iLikeWaffles


    Think I'll go 1 decimal place 0% just looks wrong

    7Weq0p1.jpg


Advertisement