[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Formating Floats as Money

John Kopanas

11/22/2006 9:43:00 PM

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.k...
http://www...
http://www...

11 Answers

David Kastrup

11/22/2006 9:44:00 PM

0

"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)

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum

Ara.T.Howard

11/22/2006 10:42:00 PM

0

David Vallner

11/22/2006 10:44:00 PM

0

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


M. Edward (Ed) Borasky

11/22/2006 11:09:00 PM

0

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_...
>
>
> -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.blo...

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


Paul Lutus

11/22/2006 11:13:00 PM

0

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.

--
Paul Lutus
http://www.ara...

John Kopanas

11/25/2006 3:48:00 AM

0

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" <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)
>
> --
> David Kastrup, Kriemhildstr. 15, 44793 Bochum
>
>


--
John Kopanas
john@kopanas.com

http://www.k...
http://www...
http://www...

Paul Lutus

11/25/2006 7:10:00 AM

0

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

--
Paul Lutus
http://www.ara...

Gavin Kistner

11/25/2006 4:54:00 PM

0

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

Vidar Hokstad

11/25/2006 10:43:00 PM

0


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_...

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

Vidar Hokstad

11/25/2006 10:48:00 PM

0


Phrogz wrote:
> 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

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