# 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

2. ### OwenGuest

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

3. ### smallpondGuest

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
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
5. ### RedGrittyBrickGuest

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
6. ### PilcrowGuest

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*

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
7. ### John BokmaGuest

John Bokma, Sep 21, 2008
8. ### brian d foyGuest

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