howto round file output name placeholder "total_cost"?
 
Notifications
Clear all

howto round file output name placeholder "total_cost"?  

  RSS
cpo
 cpo
(@cpo)
New Member
howto round file output name placeholder "total_cost"?

Hi everyone,

i use this code
{input_filename_base}_{layer_height}mm_{filament_type[0]}_{print_time}_{total_cost}€.gcode

in my file output naming. The output of the tatal_cost is alway 6 decimals (phew!) but i want it to round to just two decimals like 0.51 (not 0.509234).

I know there is a function "round()" but i just don't know how to use und where to put it.

Perhaps someone here can shed a light?

Thank you.

Napsal : 19/10/2022 12:15 pm
Neophyl
(@neophyl)
Illustrious Member
RE: howto round file output name placeholder "total_cost"?

I'll pass on what I was shown

Easy fix: use "...{int(total_weight)}g..."For more decimals use "...{int((total_weight*10.0))/(10.0)}g..." or replace the "10.0" with any order of magnitude you like.
And yes, the amount of brackets look redundant, but they are important.

The example is for the weight but it can be applied to any of the place holders.  {int((total_cost*100.0))/(100.0)} rounds it to 2 decimal places. So 6.71 for example

 

Napsal : 19/10/2022 12:28 pm
Tim Weston
(@tim-weston)
Estimable Member
RE: howto round file output name placeholder "total_cost"?

Also, just to point out that there is a small difference between int(a) and round(a).

The int(a) function will just give you the integer part and so effectively always rounds down, so 50.9234 > 50.

The round(a) function will round to the nearest integer, so 50.9234 > 51.

So perhaps the slightly better equation for the above would be: {round((total_cost*100.0))/(100.0)}

Cheers,

Tim

Napsal : 19/10/2022 1:10 pm
Ringarn67 a cpo se líbí
Neophyl
(@neophyl)
Illustrious Member
RE: howto round file output name placeholder "total_cost"?

Cheers Tim.  The example I gave above was almost verbatim cut and paste what I was shown when I asked about doing it with weight on the github project.  Of course back when I asked and the example was given Round wasn't a thing 🙂   

I probably should re-do my profiles with Round instead but for weight I'm currently using the Int calculation (+1) which  in effect rounds everything up to the next whole gram which I find more useful for working out if I have enough filament left.  For cost though I agree that Round would be better

Napsal : 19/10/2022 1:19 pm
cpo se líbí
cpo
 cpo
(@cpo)
New Member
Topic starter answered:
RE: howto round file output name placeholder "total_cost"?

Thank you both!
I will use the round-option provided by Tim as it better suits the money value.
For weight i think Neophyl is right, it makes more sense to round up to the next gram there...

Thanks again. 🙂

Napsal : 19/10/2022 1:29 pm
Tim Weston
(@tim-weston)
Estimable Member
RE: howto round file output name placeholder "total_cost"?

I also noted the existence of the digits() and zdigits() functions as well. While not applicable in the scenarios we have mentioned here, they would seemingly give you even more flexibility in how you can post-process numerical values to textual output. As ever, it all depends on the specific use case!

Napsal : 19/10/2022 1:40 pm
cpo se líbí
Beeker
(@beeker)
Active Member
RE: howto round file output name placeholder "total_cost"?

Not sure if something has changed, but I'm struggling to get the proper syntax. I've got the following line in the Print Settings>Output filename format:
{input_filename_base} - {int(total_weight)}g in {print_time}.gcode
and it bums out at the "t" in total_weight with the error: Parsing error at line 1: Cannot convert to integer. 

Seems like it's trying to do the math before the variable name has been replaced.

Any thought would be appreciated. 

Napsal : 25/10/2023 3:04 am
Neophyl
(@neophyl)
Illustrious Member
RE: howto round file output name placeholder "total_cost"?

That’s because I thin you have a minus sign in there. The - is minus, a math operator. Use underscores to separate _

Napsal : 25/10/2023 5:01 am
Beeker
(@beeker)
Active Member
RE: howto round file output name placeholder "total_cost"?

Thanks for the clue, as that led me down the right path, but it doesn't actually care what characters outside the brackets.

Solution: The error occurs when trying to Export the G Code too quickly. If you watch the "Sliced Info" box when slicing, the "Export G-Code" button is available before the slicer has finished calculating the weight, cost etc. If you are too quick and push the Export button before the info is fully populated, the error is thrown. 

Cheers!

Napsal : 25/10/2023 4:14 pm
Share: