Forum: Ruby Formating Floats as Money

Ba79eb31ceaef95e9e52f5e5e4ec6d02?d=identicon&s=25 John Kopanas (Guest)
on 2006-11-22 22:43
(Received via mailing list)
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 Kopanas
john@kopanas.com

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-11-22 23:42
(Received via mailing list)
On Thu, 23 Nov 2006, John Kopanas 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.

http://en.wikipedia.org/wiki/Fixed-point_arithmetic


-a
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-11-22 23:46
(Received via mailing list)
John Kopanas 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 Vallner
3bb23e7770680ea44a2d79e6d10daaed?d=identicon&s=25 M. Edward (Ed) Borasky (Guest)
on 2006-11-23 00:10
(Received via mailing list)
ara.t.howard@noaa.gov 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/

If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.
Cdac1bce4ec96619cde73d2f2be4e66d?d=identicon&s=25 David Kastrup (Guest)
on 2006-11-24 21:51
(Received via mailing list)
"John Kopanas" <kopanas@gmail.com> 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)
4db5dbdedcae4df2feca2f2d93330208?d=identicon&s=25 Paul Lutus (Guest)
on 2006-11-24 21:53
(Received via mailing list)
John Kopanas 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.
Ba79eb31ceaef95e9e52f5e5e4ec6d02?d=identicon&s=25 John Kopanas (Guest)
on 2006-11-25 04:50
(Received via mailing list)
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 Kastrup <dak@gnu.org> wrote:
>
--
John Kopanas
john@kopanas.com

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info
4db5dbdedcae4df2feca2f2d93330208?d=identicon&s=25 Paul Lutus (Guest)
on 2006-11-25 08:16
(Received via mailing list)
John Kopanas 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
Ec9233451f7c6ba37a83388b87a1f565?d=identicon&s=25 Phrogz (Guest)
on 2006-11-25 17:55
(Received via mailing list)
John Kopanas 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
Caf3d97ceff60d6d105c45305d34658c?d=identicon&s=25 Vidar Hokstad (Guest)
on 2006-11-25 23:45
(Received via mailing list)
ara.t.howard@noaa.gov wrote:
> On Thu, 23 Nov 2006, John Kopanas 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.
>
> 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
Caf3d97ceff60d6d105c45305d34658c?d=identicon&s=25 Vidar Hokstad (Guest)
on 2006-11-25 23:51
(Received via mailing list)
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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (austin)
on 2006-12-20 22:22
(Received via mailing list)
On 11/25/06, Vidar Hokstad <vidar.hokstad@gmail.com> 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 Sinclair has an "extensions"
library to which I contributed my solution some time back.

-austin
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.