Easiest way to do this?

G

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?
 
O

Owen

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
 
S

smallpond

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 (e-mail address 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
 
G

Guest

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


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.
 
R

RedGrittyBrick

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.
 
P

Pilcrow

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
 
B

brian d foy

Pilcrow said:
On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), "(e-mail address removed)"
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top