Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
toString incosistent behavior

Hello all,
I would like to ask for help.
The text function toString(#variable/mm) will convert 37mm to 37, but same time converts 36 mm to 36.00000…1. This drive me really mad.
Is there some reasonable way how to control number of decilmals places?
I appreciate any help.
Thanks
0
Answers
Everything is stored internally in meters, so
36 mm
is actually36 * 0.001 m
. Which results in floating-point artifacts.Use
roundToPrecision
: https://cad.onshape.com/FsDoc/library.html#roundToPrecision-number-numberThank you for suggested solution!
But the explanation does not make sense to me:
36 x 0,001 is NOT 36,000000000000001 (see screenshot)
same time
37x0,001 is NOT 37 (see screenshot)
It's not obvious off the top of my head why you're seeing these specific numbers (there might be some additional unit conversion in the middle somewhere), but it's generally true that not all numbers can be represented exactly in floating-point math; for some, we can only get close. For example,
0.1 + 0.2 = 0.30000000000000004
. If you're formatting numbers for display, you should assume that you'll get artifacts like this, and round appropriately.Thanks for the help. I did encounter another issue with this.
toString(roundToPrecision(34.5,1)) will produce string "34.5" which is expected and wanted.
toString(roundToPrecision(35,1)) will produce string "35" instead of "35.0".
Can someone please suggest what trick do I need to use to archive that string made from number will have fix number of decimals?
If your certain there are no decimals at all, you can intentionally add a really small and unique value after the rounding, like 0.019191, convert it to string, then remove the unique string.
Also, this might save you some time, try the Variable to String custom feature.
It will convert your variable with units to a string, while keeping in mind the expected units and trailing zeros.
The variable to string feature uses a more robust way to check for zeros. It splits the string into an array by checking if there is a decimal. If there is a decimal, splits the value to the right of the decimal into another array and counts the values. From there, it checks if there are too few decimals then adds accordingly:
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴