# For matz to do

**URL:** <https://www.ruby-forum.com/t/for-matz-to-do/110561>\
**Category:** Ruby\
**Created:** [July 27, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561 "2007-07-27T15:08:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 27, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561/1 "2007-07-27T15:08:28Z")

</div>

i’d like to see two new ruby language additions…  
1.) in the sprintf formatting command a way to format for business…  
i.e… processing payroll chks…with asterik fill AND commas  
and space fill AND commas or anything fill AND commas to do this  
\*\*\*\*\*$1,312.00  
without having to write a separate def function…

…and…  
i’m comming from BBX (Business Basic)… a new kind of number… like  
Bignum  
…but Biznum … where all intermediate calculations are correctly and  
consistently rounded. we have a billing system not written in BBX and  
it’s always pennies off due to round off error … there is value in  
this…because BBX has it and i’ve never had to worry about round off  
errors

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 27, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561/2 "2007-07-27T15:18:56Z")

</div>

On 7/27/07, Dave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …but Biznum … where all intermediate calculations are correctly and  
> consistently rounded. we have a billing system not written in BBX and  
> it’s always pennies off due to round off error … there is value in  
> this…because BBX has it and i’ve never had to worry about round off  
> errors

Look at BigDecimal

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 27, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561/3 "2007-07-27T15:38:58Z")

</div>

On Jul 27, 3:08 pm, Dave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i’m comming from BBX (Business Basic)… a new kind of number… like  
> Bignum  
> …but Biznum … where all intermediate calculations are correctly and  
> consistently rounded. we have a billing system not written in BBX and  
> it’s always pennies off due to round off error … there is value in  
> this…because BBX has it and i’ve never had to worry about round off  
> errors

Try this:

# prelude

require ‘rational’  
require ‘bigdecimal’  
require ‘bigdecimal/util’

# compute

result = Rational(5) / 23 \* 13 / 27

# output

precision = 5  
result.to\_d(precision).to\_s(“F”) # =\> “0.10467”

precision = 50  
result.to\_d(precision).to\_s(“F”) # =\>  
“0.10466988727858293075684380032206119162640901771337”

What this means is that you are working with maximum precision  
whenever one of the numbers involved in an operation is a Rational().

At output time you specify the precision you actually need.

Unfortunately, operations from the Math module will still convert  
their arguments to and produce floats.

It would be nice if we’d need to require less files to get that  
behaviour. I’d also prefer being able to do result.to\_s(precision)  
over .to\_d().to\_s().

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 27, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561/4 "2007-07-27T15:55:59Z")

</div>

Florian G. wrote:

> # output
> 
> precision = 5  
> result.to\_d(precision).to\_s(“F”) # =\> “0.10467”

Next, on the “for matz to do” angle, we might need a little lecture here  
on  
how easily Ruby will let you wrap your methods up in reusable classes,  
such  
as BizNum. Quite unlike a BASIC…

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 27, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561/5 "2007-07-27T18:18:28Z")

</div>

…all good points…to all above …can one also modify struct class to  
do the biznum?

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 27, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/for-matz-to-do/110561/6 "2007-07-27T16:03:00Z")

</div>

Dave R. wrote:

> …but Biznum … where all intermediate calculations are correctly and  
> consistently rounded. we have a billing system not written in BBX and  
> it’s always pennies off due to round off error … there is value in  
> this…because BBX has it and i’ve never had to worry about round off  
> errors

Being realistic here, in all likely hood you are the expert on this and  
not matz or 99.9999% of the Ruby users.

If this is important to you then at least start on the project  
(collecting the specifications and the like for Biznum, has this been  
implemented in Python or Perl which you might use as a starting point)  
and also look around for other Rubyists that are also interested in this  
problem who might help you. Set up a Rubyforge project and get things  
rolling.

Bear in mind the market for your extensions. The formatting you show  
seems to be only valid for UK or US businesses. Will this class be  
useful for French, Austrian or Greek business users, other countries  
probably have specific rules about how you do accounting (definitely  
different formatting rules) - possibly even depending on the business  
sector they operate within. This will dictate the pool of talent that  
you can draw on. If your solution will only work in the UK then people  
with a similar problem in the US are probably not going to be  
interested.

Adding extensions to the language just for business users in one or two  
countries seems to be asking a lot when you could simply create a new  
class to handle it.

Also when trying to drum up support you might consider your writing  
style, you come across as a stoner 🙂
