# IronRuby and System::Decimal

**URL:** <https://www.ruby-forum.com/t/ironruby-and-system-decimal/152745>\
**Category:** IronRuby\
**Created:** [November 7, 2008, 1:36am UTC](https://www.ruby-forum.com/t/ironruby-and-system-decimal/152745 "2008-11-07T01:36:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [November 7, 2008, 1:36am UTC](https://www.ruby-forum.com/t/ironruby-and-system-decimal/152745/1 "2008-11-07T01:36:26Z")

</div>

Hello,

Just playing around with some .Net interop and the Decimal type. When  
I do puts, I expected that this would have given me the underlying  
number, or at least call to\_string.

Instead I got the following behaviour, with the object type being  
outputted.

> > > @d = System::Decimal.MinValue  
> > > =\> #System::Decimal:0x000005c
> 
> > > puts @d  
> > > #System::Decimal:0x000005e  
> > > =\> nil
> 
> > > puts @d.to\_string  
> > > -79228162514264337593543950335  
> > > =\> nil

Is this by design?

Thanks

Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [November 7, 2008, 1:45am UTC](https://www.ruby-forum.com/t/ironruby-and-system-decimal/152745/2 "2008-11-07T01:45:48Z")

</div>

Also, how would I compare decimals in ruby, as Ruby doesn’t have a  
decimal type built in?

I wanted to do something like

@d \> ‘12312.123123’.to\_d

However, that doesn’t appear to be possible. I attempt to do to\_f,  
but I got the following error:  
ArgumentError: Object must be of type Decimal.

> From mscorlib:0:in  
> `CompareTo'E:\IronRuby\r156\src\IronRuby.Libraries\Extensions\IComparableOps.cs:24:in`\<=\>’

Any ideas?

Thanks  
Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [November 7, 2008, 1:51am UTC](https://www.ruby-forum.com/t/ironruby-and-system-decimal/152745/3 "2008-11-07T01:51:03Z")

</div>

Originally, I had hacked in “decimal” to be our BigDecimal  
representation. Now that we have a real BigDecimal (courtesy of Peter  
Bacon D.), the CLR decimal type has become an orphan – it doesn’t  
really have any direct support in IronRuby, so you just get the same  
behavior that you’d get for any other random CLR object.

Let me pose this question to the list: which classes in the BCL (if any)  
do you think should get the “Rubification” treatment?

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [November 7, 2008, 2:29am UTC](https://www.ruby-forum.com/t/ironruby-and-system-decimal/152745/4 "2008-11-07T02:29:38Z")

</div>

Here’s some helper methods I’ve used for making System::Decimal a little  
more palatable:

class Object

```
def to_clr_dec
    System::Decimal.parse(self.to_s)
end
def to_clr_int
    System::Int32.parse(self.to_s)
end

```

end

class System::Decimal  
def + other  
System::Decimal.Add(self, other)  
end

```
    def - other
        System::Decimal.Subtract(self, other)
    end

    def * other
        System::Decimal.Multiply(self, other)
    end

    def to_f
        self.to_string.to_s.to_f
    end

    def inspect
        to_f
    end

```

end

FWIW I’d personally rather see “rubification” done this way, then in C#  
libraries…
