# Easiest way to do this?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

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?

## Re: Easiest way to do this?

On Sep 19, 1:23 pm, "void.no.spam....@gmail.com"

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

## Re: Easiest way to do this?

In our last episode, the evil Dr. Lacto had captured our hero,

This seems like a useless step considering

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 hymie@lactose.homelinux.net
------------------------ Without caffeine for 689 days ------------------------

## Re: Easiest way to do this?

On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:

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

## Re: Easiest way to do this?

On Sep 19, 9:15=A0am, hymie_@_lactose.homelinux.net (hymie!) wrote:

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.

## Re: Easiest way to do this?

void.no.spam.com@gmail.com wrote:

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

## Re: Easiest way to do this?

On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), "void.no.spam.com@gmail.com"

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

## Re: Easiest way to do this?

perldoc -q ceil

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

Perl help in exchange for a gift: