Formatting numbers to currency format

Discussion in 'Perl Misc' started by Robert TV, Jun 19, 2004.

  1. Robert TV

    Robert TV Guest

    I would like to know if Perl has a function to correctly format a string of
    numbers into currency format. I have a small subroutine that takes a number
    and adds a tax amount. Below is the base:

    $amount = 100;
    $tax = 0.05 # 5% tax rate
    $finaltotal = $amount * $tax;
    $finaltotal = sprintf("%.02f",$finaltotal);

    Sometimes when then $amount is a complex number such as 1423 I will get
    several decimal places, and sprintf rounds to two decimal places. This is
    good. But when the amount like simple as seen above, there are no cedimal
    places, therefor sprintf doesnt have anything to round. I am trying to make
    it so all $finaltotals have two decimal places, even if they are .00 or .90
    etc. Can anyone help? TIA!

    Robert
     
    Robert TV, Jun 19, 2004
    #1
    1. Advertising

  2. Robert TV

    Anno Siegel Guest

    Robert TV <> wrote in comp.lang.perl.misc:
    > I would like to know if Perl has a function to correctly format a string of
    > numbers into currency format. I have a small subroutine that takes a number
    > and adds a tax amount. Below is the base:
    >
    > $amount = 100;
    > $tax = 0.05 # 5% tax rate


    There's a semicolon missing after this statement. This is not the code
    you ran.

    Don't re-type code, copy and paste it.

    > $finaltotal = $amount * $tax;


    Shouldn't that be "$amount * ($tax + 1)"?

    > $finaltotal = sprintf("%.02f",$finaltotal);
    >
    > Sometimes when then $amount is a complex number such as 1423 I will get
    > several decimal places, and sprintf rounds to two decimal places. This is
    > good. But when the amount like simple as seen above, there are no cedimal
    > places, therefor sprintf doesnt have anything to round. I am trying to make
    > it so all $finaltotals have two decimal places, even if they are .00 or .90
    > etc. Can anyone help? TIA!


    Are you saying, "sprintf '%.02f', ..." isn't showing trailing zeroes?
    It should, and it does for me.

    Show runnable code that demonstrates your problem.

    Anno
     
    Anno Siegel, Jun 19, 2004
    #2
    1. Advertising

  3. Robert TV wrote:
    > I would like to know if Perl has a function to correctly format a
    > string of numbers into currency format.


    Why did you start a new thread on this topic, instead of following up
    the replies at the thread you started a few days ago??

    http://groups.google.com/groups?threadm=

    ( Are only morons asking questions nowadays? :( )

    > $finaltotal = sprintf("%.02f",$finaltotal);


    <snip>

    > ... there are no cedimal places, therefor sprintf doesnt have
    > anything to round.


    Did you ever run any code that made you come to that conclusion? In
    that case, please show us THAT code!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ashish

    currency formatting for Euro

    ashish, Apr 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    ashish
    Apr 27, 2004
  2. Vlad

    Currency Formatting

    Vlad, May 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    4,375
  3. CD1
    Replies:
    7
    Views:
    5,706
  4. Daniele Varrazzo

    Formatting Currency According to Locale

    Daniele Varrazzo, Apr 28, 2004, in forum: Python
    Replies:
    0
    Views:
    327
    Daniele Varrazzo
    Apr 28, 2004
  5. pebelund
    Replies:
    1
    Views:
    443
    Morten Wennevik
    Oct 23, 2006
Loading...

Share This Page