# Numeric Conversion

**URL:** <https://forums.sketchup.com/t/numeric-conversion/159664>\
**Category:** Ruby API\
**Created:** [April 6, 2021, 2:01pm UTC](https://forums.sketchup.com/t/numeric-conversion/159664 "2021-04-06T14:01:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![susketch17](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@susketch17](https://forums.sketchup.com/u/susketch17)\
**Post date:** [April 6, 2021, 2:01pm UTC](https://forums.sketchup.com/t/numeric-conversion/159664/1 "2021-04-06T14:01:10Z")

</div>

Hi.,  
Usually `.cm ` operator converts centimeter values to inches. irrespective of the unit setup in the sketchup or it will behave based on the unit setup

But i am getting output like this. `10.cm` shows 100mm

 ![Capture1](https://global.discourse-cdn.com/sketchup/original/3X/0/f/0ff2f479679c67e8c0c35f3bf444ef367d240ba0.png)

Thanks

---

<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:** [April 6, 2021, 2:44pm UTC](https://forums.sketchup.com/t/numeric-conversion/159664/2 "2021-04-06T14:44:09Z")

</div>

Your first numerical input is taken as inches,  
so 10.to\_cm returns 25.4, i.e. that’s 10" in cm  
then 10.cm is taken a numerical 10 [not inches] converted into current units [mm] =\> 100.0mm

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [April 6, 2021, 2:49pm UTC](https://forums.sketchup.com/t/numeric-conversion/159664/3 "2021-04-06T14:49:25Z")

</div>

For starters, you need to understand that SketchUp’s Ruby has a subclass “Length” derived from Numeric. This class mainly just tags a Numeric so that SketchUp knows it was meant to represent a length vs just being a number. The actual units of a Length are always inches.

The setting in Model Info-\>Units specifies the format that SketchUp will use when displaying any Length value on the GUI or taking input from the measurements box. So, for display, mm units with precision .0 tells SketchUp to **display** a length that internally is 10 inches by calculating its equivalent in mm and rounding to one decimal place. It does not change the internal value, just says how to format it.

Regarding the .cm method, SketchUp Ruby added this to Numeric to tell the interpreter that the string literal “10.cm” in the code is to be taken as “the raw number 10 was meant to be cm, so convert it accordingly when generating the numeric value of Length to store”. You might mentally rename the method “.from\_cm”. The .to\_cm method is a value conversion method. It tells the interpreter “generate the numeric value for the number of cm equivalent to this number in inches”. Note that this does not require that 10 be already a Length, just a number, and does not imply that the result is a Length.

To convince yourself of these things, try this:

```ruby
10.class # -> Integer
10.cm.class # -> Length
10.to_cm.class # -> Float

```

---

<div class="post-metadata">

**Author:** ![susketch17](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@susketch17](https://forums.sketchup.com/u/susketch17)\
**Post date:** [April 6, 2021, 3:29pm UTC](https://forums.sketchup.com/t/numeric-conversion/159664/4 "2021-04-06T15:29:48Z")

</div>

@TIG / @slbaumgartner . Thanks for the detailed explanation.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 6, 2021, 4:10pm UTC](https://forums.sketchup.com/t/numeric-conversion/159664/5 "2021-04-06T16:10:42Z")

</div>

> [@slbaumgartner](#):
>
> So, for display, mm units with precision .0 tells SketchUp to **display** a length that internally is 10 inches by calculating its equivalent in mm and rounding to one decimal place. It does not change the internal value, just says how to format it.

@susketch17 Note that the output to the Ruby Console is **by default** run through the **`to_s`** method for the class of the object (or result.) So you see the value in model units because the result is of class [`Length`](https://ruby.sketchup.com/Length.html) and this class has an overridden [`#to_s`](https://ruby.sketchup.com/Length.html#to_s-instance_method) method that displays a string in model units.

But to see the real value, use the `inspect` method …

```ruby
10.cm.inspect
#=> 3.937007874015748

```

So you see that the value is actually internally in inches.

---

<div class="post-metadata">

**Author:** ![susketch17](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@susketch17](https://forums.sketchup.com/u/susketch17)\
**Post date:** [April 7, 2021, 7:15am UTC](https://forums.sketchup.com/t/numeric-conversion/159664/6 "2021-04-07T07:15:09Z")

</div>

Thanks @DanRathbun
