Velocity Reviews > Perl > Easiest way to do this?

# Easiest way to do this?

void.no.spam.com@gmail.com
Guest
Posts: n/a

 09-19-2008
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?

Owen
Guest
Posts: n/a

 09-19-2008
On Sep 19, 1:23 pm, "(E-Mail Removed)"
<(E-Mail Removed)> 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

smallpond
Guest
Posts: n/a

 09-19-2008
On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:
> In our last episode, the evil Dr. Lacto had captured our hero,
> <(E-Mail Removed)>, 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 (E-Mail Removed)
> ------------------------ 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

void.no.spam.com@gmail.com
Guest
Posts: n/a

 09-19-2008
On Sep 19, 9:15*am, hymie_@_lactose.homelinux.net (hymie!) wrote:
> In our last episode, the evil Dr. Lacto had captured our hero,
> * <(E-Mail Removed)>, 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* *(E-Mail Removed)
> ------------------------ Without caffeine for 689 days ------------------------

RedGrittyBrick
Guest
Posts: n/a

 09-19-2008

http://www.velocityreviews.com/forums/(E-Mail Removed) 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,
>> <(E-Mail Removed)>, 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

Pilcrow
Guest
Posts: n/a

 09-21-2008
On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), "(E-Mail Removed)"
<(E-Mail Removed)> 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,
>> * <(E-Mail Removed)>, 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

John Bokma
Guest
Posts: n/a

 09-21-2008
Pilcrow <(E-Mail Removed)> wrote:

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

perldoc -q ceil

--
John http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:

brian d foy
Guest
Posts: n/a

 09-21-2008
In article <(E-Mail Removed)>, Pilcrow
<(E-Mail Removed)> wrote:

> On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), "(E-Mail Removed)"
> <(E-Mail Removed)> 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.