# Alternative coding for decimal arithmetic

**URL:** <https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888>\
**Category:** Rails\
**Created:** [April 30, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888 "2009-04-30T15:30:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 30, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888/1 "2009-04-30T15:30:33Z")

</div>

I need to transform a decimal fraction received as a string into its  
reciprocal, also stored as a string. I have come up with this but I  
would like to know if there exists a better way to accomplish this  
requirement.

```
  trate = xchg.xpath('cb:value').text
  scale = 10 ** (trate.length() -1 - (trate.index('.').to_i))
  value = trate.gsub(/\.|,| /,'').to_i
  trate = (1000000 * scale / value).to_s
  trate = trate.rjust(7,'0')
  trate = trate.insert(-7,'.')
```

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 30, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888/2 "2009-04-30T15:51:35Z")

</div>

Can’t you just do something like  
(1/ (trate.to\_f)).to\_s  
I expect you don’t need all the parantheses1

2009/4/30 James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 30, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888/3 "2009-04-30T16:14:21Z")

</div>

Colin L. wrote:

> Can’t you just do something like  
> (1/ (trate.to\_f)).to\_s  
> I expect you don’t need all the parantheses1

I cannot drop to floating point. It has to be integer arithmetic.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 30, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888/4 "2009-04-30T17:06:25Z")

</div>

James B. wrote:

> I cannot drop to floating point. It has to be integer arithmetic.

Use BigDecimal (part of the standard library). It does  
arbitrary-precision decimal arithmetic without the problems of floats.  
I think the syntax would be something like

(BigDecimal.new(1) / BigDecimal.new(trate)).to\_s

But check the docs to be sure. There’s certainly no need to jump  
through the hoops you’re going through right now.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 30, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/alternative-coding-for-decimal-arithmetic/166888/5 "2009-04-30T18:19:07Z")

</div>

Marnen Laibow-Koser wrote:

> Use BigDecimal (part of the standard library). It does  
> arbitrary-precision decimal arithmetic without the problems of floats.  
> I think the syntax would be something like

Thanks, that works much better.
