# Why unit conversion doesn't work?

**URL:** <https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970>\
**Category:** Ruby API\
**Created:** [September 9, 2021, 6:02pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970 "2021-09-09T18:02:08Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [September 14, 2021, 9:41am UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/21 "2021-09-14T09:41:28Z")

</div>

> [@eneroth3](#):
>
> This approach does not work. “10,5” would be red as 10, because String#to\_f doesn’t honor the user’s chosen decimal sign. Also it doesn’t differentiate “10 m”, “10 cm” and “10””.

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.

> [@eneroth3](#):
>
> Only use floats to serialize the value for storage. In ALL other cases, use Length for length values.

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.

---

<div class="post-metadata">

**Author:** ![JDL](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@JDL](https://forums.sketchup.com/u/JDL)\
**Post date:** [January 23, 2022, 2:54pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/22 "2022-01-23T14:54:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [January 23, 2022, 3:01pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/23 "2022-01-23T15:01:47Z")

</div>

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 !!

---

<div class="post-metadata">

**Author:** ![JDL](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@JDL](https://forums.sketchup.com/u/JDL)\
**Post date:** [January 23, 2022, 3:06pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/24 "2022-01-23T15:06:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [January 23, 2022, 3:17pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/25 "2022-01-23T15:17:58Z")

</div>

Learn about the ‘Length’ Class in the API guide.  
[https://ruby.sketchup.com/Length.html](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 ?

---

<div class="post-metadata">

**Author:** ![JDL](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@JDL](https://forums.sketchup.com/u/JDL)\
**Post date:** [January 23, 2022, 3:21pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/26 "2022-01-23T15:21:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [January 23, 2022, 3:26pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/27 "2022-01-23T15:26:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [January 23, 2022, 4:21pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/28 "2022-01-23T16:21:24Z")

</div>

> [@JDL](#):
>
> I would like to understand why 41’9" comes out as 12700 mm instead of 12725 mm.

![unit](https://global.discourse-cdn.com/sketchup/original/3X/f/c/fc053260ec3393b33a1d415237b7c0062e57ac70.gif)

---

<div class="post-metadata">

**Author:** ![JDL](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@JDL](https://forums.sketchup.com/u/JDL)\
**Post date:** [January 23, 2022, 4:27pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/29 "2022-01-23T16:27:18Z")

</div>

I found out the problem. when I write (42’ 7 1/4") as (42_12+7+1/4), the last component 1/4 doesn’t get accounted. If i write (42_12+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.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [January 23, 2022, 5:15pm UTC](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970/30 "2022-01-23T17:15:55Z")

</div>

Maybe you are using improper single quote ’ (and/or double quotes ")  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/e/0/e0dba4a0c822ffabf7e0116ddbf1ca611447f0fc.png)

Because for me it works:  
 ![qutes](https://global.discourse-cdn.com/sketchup/original/3X/0/3/03ffcc988dbe5ae648a54fecf99eaccd0f980774.gif)

[Previous page](https://forums.sketchup.com/t/why-unit-conversion-doesnt-work/174970.md?page=1)
