Why unit conversion doesn't work?

You wouldn’t get “10,5” is you wrote the Length to file by first converting it to Float. Float#to_s will always use period for decimal separator.

Then, when you read the file, you have the unit “10.5” as a string, which you convert to a Float, which also will always work because String.to_f always expect period as decimal separator.

Most often people serialize by converting the float to string. It’s simplest. Though you could serialize by using pack/unpack but the code becomes harder to read, I’d only do that if I had some good reason to serialize to binary format.

Hello, I am trying to model using Ruby and measurements in inches. However in my model, the distances are given in meters, and the conversion is not correct.
Can anyone help me sort this out.

Model Info > Units sets the model’s units.
Try changing it to ‘meters’…
SketchUp always ‘remembers’ its data in inches, but the model’s unit setting displays units in the chosen type.
You do not need to type a unit-suffix when entering dimensions, because the model’s units are assumed.
However, adding units overwrites this so if you are working in meters 1 is 1m but typing 10mm is 0.01m

Do not click on the VCB when entering values, just type them.

Also work mentioning is the ‘decimal-separator’…
Usually a . in the USA/UK/IR but in many other places like the EU it’s a ,
This means that the list-separator of , changes to ; to avoid confusion !!

Actually in the ruby extension the I am writing to draw automatically, I define a length equal to 100 (I understand that this is in inches by default). Then in my model, when I run the extension, and measure the length in millimetres , I don’t get 100 * 25.4 = 2540 mm. That’s what I would like to fix.

Learn about the ‘Length’ Class in the API guide.
https://ruby.sketchup.com/Length.html

What you’ve said in expected 100 [inches] is 2540mm and will display as such when your model-units are in mm - so what’s the issue ??
If you want it to be 100m say so in your code using 100.m rather than mess with inches at all ?

I would like to understand why 41’9" comes out as 12700 mm instead of 12725 mm.

Now you move the goal-posts again !
How are you inputting the value 41’9" ?
How is SketchUp ‘interpreting’ that into mm ?
Without examples of your code this is pure guesswork…

unit

I found out the problem. when I write (42’ 7 1/4") as (4212+7+1/4), the last component 1/4 doesn’t get accounted. If i write (4212+7+0.25), it works. This may be related to integer division. If you know how to solve it, let me know. I have dimensions like 1/32 and I would like the machine to transform this into a real number.

Maybe you are using improper single quote ’ (and/or double quotes ")
image

Because for me it works:
qutes

1 Like