Rounding up to 2 decimal places

G

Gary Mayor

Hi,
What a nightmare i've been trawling deja all morning for a simple method
of rounding a number up to 2 decimal places. I've checked out the ceil
function which only rounds numbers up to no decimal places which is no
good. The printf function rounds numbers up and down.

Let's take this number

0.3325

I need it to be,

0.34

How do i do that. Do I have to write a routine that checks if there is a
third digit or what. The number is calculated for the UK VAT system at
17.5% i need the number rounded up to only 2 decimal places. Yes I have
checked perlfaq4.

Simple problem seems impossible answer.

Any ideas

Gary
 
A

Anno Siegel

Gary Mayor said:
Hi,
What a nightmare i've been trawling deja all morning for a simple method
of rounding a number up to 2 decimal places. I've checked out the ceil
function which only rounds numbers up to no decimal places which is no
good.

Then help it along.
The printf function rounds numbers up and down.

Let's take this number

0.3325

I need it to be,

0.34

How do i do that. Do I have to write a routine that checks if there is a
third digit or what. The number is calculated for the UK VAT system at
17.5% i need the number rounded up to only 2 decimal places. Yes I have
checked perlfaq4.

Financial calculations are best done in the smallest unit of currency.
Multiply by 100 on input, divide by 100 on output. That would make
the problem with ceil() go away, because you only *have* integers.
Simple problem seems impossible answer.

ceil( 100*$x)/100

Oh, and you could put question marks after your questions. You sound
over-assertive without them :)

Anno
 
E

Eric J. Roode

Hi,
What a nightmare i've been trawling deja all morning for a simple method
of rounding a number up to 2 decimal places. I've checked out the ceil
function which only rounds numbers up to no decimal places which is no
good. The printf function rounds numbers up and down.

Let's take this number

0.3325

I need it to be,

0.34

How do i do that. Do I have to write a routine that checks if there is a
third digit or what. The number is calculated for the UK VAT system at
17.5% i need the number rounded up to only 2 decimal places. Yes I have
checked perlfaq4.

Simple problem seems impossible answer.

Multiply by 100, ceil, divide by 100? That's the first thing that occurs
to me.
 
G

Gary Mayor

You total star. I was never good at maths anyway. That works a treat. I
don't put question marks after questions because on a few occasions i've
been told question marks look to something or another. So what do I do
with or without question marks mmmmm looks like it depends where i am.

Thank you very much

Gary
 
A

at

Hi,
What a nightmare i've been trawling deja all morning for a simple method
of rounding a number up to 2 decimal places. I've checked out the ceil
function which only rounds numbers up to no decimal places which is no
good. The printf function rounds numbers up and down.

Let's take this number

0.3325

I need it to be,

0.34

How do i do that. Do I have to write a routine that checks if there is a
third digit or what. The number is calculated for the UK VAT system at
17.5% i need the number rounded up to only 2 decimal places. Yes I have
checked perlfaq4.

Simple problem seems impossible answer.

Any ideas

Gary

You might want to try something as simple as this:

my $VALUE=0.3325;
$value=sprintf("%.2f",$VALUE);
print "value=$value\n";

Good luck.

Bob
 
W

Walt Mankowski

You might want to try something as simple as this:

my $VALUE=0.3325;
$value=sprintf("%.2f",$VALUE);
print "value=$value\n";

Er, that doesn't work. Gary said he always wants it to round *up*.
Your code rounds *down* and prints out 0.33.

The trick I've always used in situations like this is to add 0.005 to it
before passing it to printf.

Walt
 
A

Anno Siegel

Walt Mankowski said:
On 2003-12-10, R.Mariotti(at)FinancialDataCorp.com (Bob Mariotti) <> wrote:

Er, that doesn't work. Gary said he always wants it to round *up*.
Your code rounds *down* and prints out 0.33.

The trick I've always used in situations like this is to add 0.005 to it
before passing it to printf.

That (sprintf "%.2f", 0.33 + 0.005) rounds 0.33 up to 0.34, whereas
ceil( 100*0.33)/100 is 0.33. This stuff is tricky, and it's best to
use the standard library functions if available.

Anno
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top