Formating Floats as Money

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? :slight_smile:


John K.
[email protected]

http://www.kopanas.com

http://www.soen.info

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? :slight_smile:

never, never, never use floats to represent money.

-a

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? :slight_smile:

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

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

David V.

[email protected] wrote:

-a
Especially when the language has “BigDecimal” built in. :slight_smile:

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.

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? :slight_smile:

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.

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

?

:slight_smile:

On 11/24/06, David K. [email protected] wrote:


John K.
[email protected]

http://www.kopanas.com

http://www.soen.info

“John K.” [email protected] 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? :slight_smile:

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

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

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? :slight_smile:

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

[email protected] 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? :slight_smile:

never, never, never use floats to represent money.

Fixed-point arithmetic - Wikipedia

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

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

On 11/25/06, Vidar H. [email protected] 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