I knew by far that string handling in pd was not easy, and now it is my turn to be stumbled.
Input numbers can be as low as 0.5 and as big as 15000, and I'd like to make a symbol out of it, max 5 characters long.
Anyone ?
Thanx
cropping 5 leftmost digits of a float value and make a symbol out of it
I knew by far that string handling in pd was not easy, and now it is my turn to be stumbled.
Input numbers can be as low as 0.5 and as big as 15000, and I'd like to make a symbol out of it, max 5 characters long.
Anyone ?
Thanx
Hi nau,
Try this:
Basically I am using a conditional [expr] and some math in order to eliminate the unwanted digits, then I am turning them into a symbol via [makefilename].
Let me know if this is what you are looking for and take care,
Gilberto
Nice, I have to admit I've been a bit lazy here, thank you !
Nau
even changed int(x) to int(x+0.49999) in order to get rounding instead of cropping
That's very clever. And if one uses [expr if ($f1>=0, int($f1+0.4999999999999), int($f1-0.4999999999999))] then it also rounds negative numbers correctly.
Take care,
Gilberto
BTW, I created a vanilla-friendly abstraction of this rounding object dealing with several digits and negative numbers as well. See: http://forum.pdpatchrepo.info/topic/8945/simple-abstraction-to-round-numbers/3
Take care,
Gilberto
Thanks to [PD-dev] [ pure-data-Patches-3380575 ] [makefilename] patch reliably segfaults and Symbol to float conversion | PURE DATA forum~, I found that [makefilename]
actually supports C-style printf
format specifier (but only one of them per object!), so that can actually be used for the purpose - for example like in this patch, formatspec.pd:
The format specifier %.4g
reduces the number of decimals - and it will even print out a plain integer '5' in case of 5.000000 ; but careful, g
will also kick in with scientific notation for very small numbers, so for 0.0000345345234 you'd get '3.45345e-05'; alternatively, try %.3f
.
@sdaau_ml This is fantastic, thanks for sharing it! I was not aware [makefilename] was that flexible, nor did I know [fromsymbol] and [tosymbol] objects.
Take care,
Gilberto
Oops! Looks like something went wrong!