Easiest way to do this?

Discussion in 'Perl Misc' started by void.no.spam.com@gmail.com, Sep 19, 2008.

  1. Guest

    I need to be able to take any dollar amount, such as 25103.34, and
    multiply it by 0.0000056. Then I need to take the result and truncate
    everything after the fifth decimal place, so if the result is
    0.140578704, then I will have 0.14057. Then I need to round up to the
    nearest cent (ceiling function), so if I have 0.14057, then the result
    will be 0.15. What would be the easiest way to achieve this?
    , Sep 19, 2008
    #1
    1. Advertising

  2. Owen Guest

    On Sep 19, 1:23 pm, ""
    <> wrote:
    > I need to be able to take any dollar amount, such as 25103.34, and
    > multiply it by 0.0000056. Then I need to take the result and truncate
    > everything after the fifth decimal place, so if the result is
    > 0.140578704, then I will have 0.14057. Then I need to round up to the
    > nearest cent (ceiling function), so if I have 0.14057, then the result
    > will be 0.15. What would be the easiest way to achieve this?



    I am not sure I would do what you are doing. I would;

    convert money amount to cents
    use ceil function from POSIX to round up after multiplication
    then use sprintf to bring it back to $s and cents



    Owen
    Owen, Sep 19, 2008
    #2
    1. Advertising

  3. smallpond Guest

    On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:
    > In our last episode, the evil Dr. Lacto had captured our hero,
    > <>, who said:
    >
    > >I need to be able to take any dollar amount, such as 25103.34, and
    > >multiply it by 0.0000056.
    > >Then I need to take the result and truncate
    > >everything after the fifth decimal place, so if the result is
    > >0.140578704, then I will have 0.14057.

    >
    > This seems like a useless step considering
    >
    > > Then I need to round up to the
    > >nearest cent (ceiling function), so if I have 0.14057, then the result
    > >will be 0.15. What would be the easiest way to achieve this?

    >
    > Convert everything into integers before doing any math.
    >
    > Multiply
    > 2510334 * 56 = 140578704
    > (note that you've shifted 9 decimal places)
    >
    > Truncate (?)
    > int (140578704 / 10000) = 14057
    > (you've unshifted 4 decimal places)
    >
    > Round up
    > int ((14057 + 999) / 1000) = 15
    > (you've unshifted 3 decimal places)
    >
    > Convert back to dollars-and-cents
    > 15 / 100 = 0.15
    > (you've unshifted 2 decimal places)
    >
    > --hymie! http://lactose.homelinux.net/~hymie
    > ------------------------ Without caffeine for 689 days ------------------------


    As pointed out, monetary calculation is best done as
    all integer arithmetic. You can save a step in the
    above procedure by only truncating once:

    multiply
    2510334 * 56 = 140578704

    round
    int ((140578704 + 9990000) / 10000000) = 15


    --S
    smallpond, Sep 19, 2008
    #3
  4. Guest

    On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:
    > In our last episode, the evil Dr. Lacto had captured our hero,
    >   <>, who said:
    >
    > >I need to be able to take any dollar amount, such as 25103.34, and
    > >multiply it by 0.0000056.  
    > >Then I need to take the result and truncate
    > >everything after the fifth decimal place, so if the result is
    > >0.140578704, then I will have 0.14057.

    >
    > This seems like a useless step considering


    Thanks for the answer. That step actually isn't useless, because if
    the multiplication results in something like 0.140000001, and then you
    don't truncate, the final result will be 0.15 when it should be 0.14.


    > >  Then I need to round up to the
    > >nearest cent (ceiling function), so if I have 0.14057, then the result
    > >will be 0.15.  What would be the easiest way to achieve this?

    >
    > Convert everything into integers before doing any math.
    >
    > Multiply
    > 2510334 * 56 = 140578704
    > (note that you've shifted 9 decimal places)
    >
    > Truncate (?)
    > int (140578704 / 10000) =  14057
    > (you've unshifted 4 decimal places)
    >
    > Round up
    > int ((14057 + 999) / 1000) = 15
    > (you've unshifted 3 decimal places)
    >
    > Convert back to dollars-and-cents
    > 15 / 100 = 0.15
    > (you've unshifted 2 decimal places)
    >
    > --hymie!    http://lactose.homelinux.net/~hymie   
    > ------------------------ Without caffeine for 689 days ------------------------
    , Sep 19, 2008
    #4
  5. wrote:
    > On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:
    >> In our last episode, the evil Dr. Lacto had captured our hero,
    >> <>, who said:
    >>
    >>> I need to be able to take any dollar amount, such as 25103.34, and
    >>> multiply it by 0.0000056.
    >>> Then I need to take the result and truncate
    >>> everything after the fifth decimal place, so if the result is
    >>> 0.140578704, then I will have 0.14057.

    >> This seems like a useless step considering

    >
    > Thanks for the answer. That step actually isn't useless, because if
    > the multiplication results in something like 0.140000001, and then you
    > don't truncate, the final result will be 0.15 when it should be 0.14.
    >


    I don't understand what you mean, because ...

    >perl -e "print 25000.00002 * 0.0000056"

    0.140000000112

    >perl -e "printf '%.2f', 25000.00002 * 0.0000056"

    0.14

    From perldoc -f sprintf:
    # Round number to 3 digits after decimal point
    $rounded = sprintf("%.3f", $number);

    So no truncation there.


    --
    RGB
    RedGrittyBrick, Sep 19, 2008
    #5
  6. Pilcrow Guest

    On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), ""
    <> wrote:

    >On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:
    >> In our last episode, the evil Dr. Lacto had captured our hero,
    >>   <>, who said:
    >>
    >> >I need to be able to take any dollar amount, such as 25103.34, and
    >> >multiply it by 0.0000056.  
    >> >Then I need to take the result and truncate
    >> >everything after the fifth decimal place, so if the result is
    >> >0.140578704, then I will have 0.14057.

    >>
    >> This seems like a useless step considering

    >
    >Thanks for the answer. That step actually isn't useless, because if
    >the multiplication results in something like 0.140000001, and then you
    >don't truncate, the final result will be 0.15 when it should be 0.14.
    >


    you *did* say 'ceiling function'? So ceil(0.14) would be 0.15, *if*
    perl had a ceiling function.

    So, here it is:

    -----------------------------------------------------------------
    use strict; use warnings;
    while(<DATA>){
    last if /^$/;
    chomp;
    printf "%15s -> %.2f\n", $_, $_ * 0.0000056 + 0.005;
    }
    __DATA__
    25103.34
    0.01
    1.56
    345.12
    1234567.89
    9876543.21
    789012345678.90


    -----------------------------------------------------------------
    OUTPUT:

    25103.34 -> 0.15
    0.01 -> 0.01
    1.56 -> 0.01
    345.12 -> 0.01
    1234567.89 -> 6.92
    9876543.21 -> 55.31
    789012345678.90 -> 4418469.14
    Pilcrow, Sep 21, 2008
    #6
  7. John Bokma Guest

    John Bokma, Sep 21, 2008
    #7
  8. brian d foy Guest

    In article <>, Pilcrow
    <> wrote:

    > On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), ""
    > <> wrote:
    >


    > you *did* say 'ceiling function'? So ceil(0.14) would be 0.15, *if*
    > perl had a ceiling function.


    Well, Perl has the ceil() that comes with the POSIX module, which comes
    in the Standard Library.
    brian d foy, Sep 21, 2008
    #8
    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. tom
    Replies:
    4
    Views:
    14,061
    jpowers5882
    Oct 24, 2008
  2. =?Utf-8?B?S2VubmV0aA==?=

    Easiest way to bind a grid datasource to a datatable

    =?Utf-8?B?S2VubmV0aA==?=, Jan 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    6,691
    =?Utf-8?B?S2VubmV0aA==?=
    Jan 18, 2004
  3. =?Utf-8?B?QmlsbCBCb3Jn?=

    Easiest way to get page name from error routine?

    =?Utf-8?B?QmlsbCBCb3Jn?=, Mar 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    432
    Tommy
    Mar 3, 2004
  4. Henry
    Replies:
    1
    Views:
    378
    Frank Wisniewski
    Jul 21, 2004
  5. DanB
    Replies:
    8
    Views:
    522
Loading...

Share This Page