# Formating Floats as Money

**URL:** <https://www.ruby-forum.com/t/formating-floats-as-money/76970>\
**Category:** Ruby\
**Created:** [November 22, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970 "2006-11-22T22:43:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 22, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/1 "2006-11-22T22:43:54Z")

</div>

I have a number that is of type float. I want to format it as dollars  
and cents. How do you recommend I do it? 🙂

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 22, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/2 "2006-11-22T23:42:07Z")

</div>

On Thu, 23 Nov 2006, John K. wrote:

> I have a number that is of type float. I want to format it as dollars  
> and cents. How do you recommend I do it? 🙂

never, never, never use floats to represent money.

> **[Fixed-point arithmetic](https://en.wikipedia.org/wiki/Fixed-point_arithmetic)**
>
> In computing, fixed-point is a method of representing fractional (non-integer) numbers by storing a fixed number of digits of their fractional part. Dollar amounts, for example, are often stored with exactly two fractional digits, representing the cents (1/100 of dollar). More generally, the term may refer to representing fractional values as integer multiples of some fixed small unit, e.g. a fractional amount of hours as an integer multiple of ten-minute intervals. Fixed-point number represe...

-a

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 22, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/3 "2006-11-22T23:46:07Z")

</div>

John K. wrote:

> I have a number that is of type float. I want to format it as dollars  
> and cents. How do you recommend I do it? 🙂

An example wouldn’t hurt for the non-american _cough_ minority.

Is “$%.2f”.format(moniez) what you need?

David V.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 23, 2006, 12:10am UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/4 "2006-11-23T00:10:11Z")

</div>

removed\_email\_address@domain.invalid wrote:

> -a  
> _Especially_ when the language has “BigDecimal” built in. 🙂

However … be aware that if you need to deal with compound interest  
calculations, they are difficult to do in most decimal arithmetic  
implementations, so at that point, you often need to bite the floating  
point (and exponential/logarithm) bullet. That’s why HP still makes the  
12C and still sells it by the carload today – it does everything  
exactly the way people expect it to be done.

Also, see the other thread about just how badly Excel and VBA do this  
sort of thing.

–  
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)  
[http://borasky-research.blogspot.com/](http://borasky-research.blogspot.com/)

If God had meant for carrots to be eaten cooked, He would have given  
rabbits fire.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 24, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/5 "2006-11-24T21:53:07Z")

</div>

John K. wrote:

> I have a number that is of type float. I want to format it as dollars  
> and cents. How do you recommend I do it? 🙂

Do you want delimiters between each group of three digits? As in:

$123,456,789.33 # US style

$123.456.789,33 # European style

If not, the replies you have gotten show how to get two decimal places  
after  
the decimal point.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 25, 2006, 4:50am UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/6 "2006-11-25T04:50:13Z")

</div>

ahhh… yes… now how do we take it up a notch and divide thousand  
from hundreds… for example:

$10,343 or better yet $1,234,234.32

?

🙂

On 11/24/06, David K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 24, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/7 "2006-11-24T21:51:07Z")

</div>

“John K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I have a number that is of type float. I want to format it as dollars  
> and cents. How do you recommend I do it? 🙂

format(“$%.2f”,3.3)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 25, 2006, 8:16am UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/8 "2006-11-25T08:16:28Z")

</div>

John K. wrote:

> ahhh… yes… now how do we take it up a notch and divide thousand  
> from hundreds… for example:
> 
> $10,343 or better yet $1,234,234.32

This approach ignores a certain locale issue, but:

* * *

#!/usr/bin/ruby -w

class String  
def currency\_format()  
while self.sub!(/(\d+)(\d\d\d)/,’\1,\2’); end  
self  
end  
end

## 0.upto(20) do |n| x = 10 \*\* n xs = x.to\_s + “.33” puts xs.currency\_format().rjust(30) end

```
                      1.33
                     10.33
                    100.33
                  1,000.33
                 10,000.33
                100,000.33
              1,000,000.33
             10,000,000.33
            100,000,000.33
          1,000,000,000.33
         10,000,000,000.33
        100,000,000,000.33
      1,000,000,000,000.33
     10,000,000,000,000.33
    100,000,000,000,000.33
  1,000,000,000,000,000.33
 10,000,000,000,000,000.33
100,000,000,000,000,000.33

```

1,000,000,000,000,000,000.33  
10,000,000,000,000,000,000.33  
100,000,000,000,000,000,000.33

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 25, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/9 "2006-11-25T17:55:22Z")

</div>

John K. wrote:

> I have a number that is of type float. I want to format it as dollars  
> and cents. How do you recommend I do it? 🙂

Here’s my shot at it:

class Numeric  
def to\_currency( pre\_symbol=’$’, thousands=’,’, decimal=’.’,  
post\_symbol=nil )  
“#{pre\_symbol}#{  
( “%.2f” % self ).gsub(  
/(\d)(?=(?:\d{3})+(?:$|.))/,  
“\1#{thousands}”  
)  
}#{post\_symbol}”  
end  
end

[  
1,21,321,4321,54321,654321,7654321,87654321,  
1.5,21.5,321.5,4321.5,54321.5,654321.5,  
].each{ |n|  
puts “%14s -\> %s” % [n.to\_s, n.to\_currency]  
puts “%14s -\> %s” % [(-n).to\_s, (-n).to\_currency]  
}

#=\> 1 -\> $1.00  
#=\> -1 -\> $-1.00  
#=\> 21 -\> $21.00  
#=\> -21 -\> $-21.00  
#=\> 321 -\> $321.00  
#=\> -321 -\> $-321.00  
#=\> 4321 -\> $4,321.00  
#=\> -4321 -\> $-4,321.00  
#=\> 54321 -\> $54,321.00  
#=\> -54321 -\> $-54,321.00  
#=\> 654321 -\> $654,321.00  
#=\> -654321 -\> $-654,321.00  
#=\> 7654321 -\> $7,654,321.00  
#=\> -7654321 -\> $-7,654,321.00  
#=\> 87654321 -\> $87,654,321.00  
#=\> -87654321 -\> $-87,654,321.00  
#=\> 1.5 -\> $1.50  
#=\> -1.5 -\> $-1.50  
#=\> 21.5 -\> $21.50  
#=\> -21.5 -\> $-21.50  
#=\> 321.5 -\> $321.50  
#=\> -321.5 -\> $-321.50  
#=\> 4321.5 -\> $4,321.50  
#=\> -4321.5 -\> $-4,321.50  
#=\> 54321.5 -\> $54,321.50  
#=\> -54321.5 -\> $-54,321.50  
#=\> 654321.5 -\> $654,321.50  
#=\> -654321.5 -\> $-654,321.50

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 25, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/10 "2006-11-25T23:45:57Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Thu, 23 Nov 2006, John K. wrote:
> 
> > I have a number that is of type float. I want to format it as dollars  
> > and cents. How do you recommend I do it? 🙂
> 
> never, never, never use floats to represent money.
> 
> [Fixed-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Fixed-point_arithmetic)

I’m very happy someone said that. But to expand on at least one reason  
why:

In many countries the taxman will be very unhappy, as things like  
VAT/sales tax often have _very_ specific rules about how rounding and  
fractions should be handled (i.e. UK regulations specify two  
alternative methods that it’s your responsibility to ensure your  
results matches to the penny if you generate VAT invoices).

Vidar

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 25, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/11 "2006-11-25T23:51:57Z")

</div>

Phrogz wrote:

> ```
> ( "%.2f" % self ).gsub(
> /(\d)(?=(?:\d{3})+(?:$|\.))/,
> "\\1#{thousands}"
> )
> }#{post_symbol}"
> 
> ```
> 
> end  
> end

Nice try. Now all it needs is to be able to handle other variations of  
the number of digits between each separator, which may not be uniform  
(i.e. 100,00,000.00), and it should be able to handle most  
international formats.

Vidar

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 20, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/formating-floats-as-money/76970/12 "2006-12-20T22:22:16Z")

</div>

On 11/25/06, Vidar H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > "#{pre_symbol}#{
> > 
> > ```
> 
> (i.e. 100,00,000.00), and it should be able to handle most  
> international formats.

There’s one of those on Rubyforge. Gavin S. has an “extensions”  
library to which I contributed my solution some time back.

-austin
