Velocity Reviews > Perl > Rounding up to 2 decimal places

# Rounding up to 2 decimal places

Gary Mayor
Guest
Posts: n/a

 12-09-2003
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

Anno Siegel
Guest
Posts: n/a

 12-09-2003
Gary Mayor <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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

Eric J. Roode
Guest
Posts: n/a

 12-09-2003
Gary Mayor <(E-Mail Removed)> wrote in news:br4etq\$nd8\$1
@newsg1.svr.pol.co.uk:

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

--
Eric
\$_ = reverse sort \$ /. r , qw p ekca lre uJ reh
ts p , map \$ _. \$ " , qw e p h tona e and print

Gary Mayor
Guest
Posts: n/a

 12-09-2003
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

Anno Siegel wrote:
> Gary Mayor <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>>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

at
Guest
Posts: n/a

 12-10-2003
On Tue, 09 Dec 2003 12:26:35 +0000, Gary Mayor <(E-Mail Removed)>
wrote:

>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

Walt Mankowski
Guest
Posts: n/a

 12-10-2003
On 2003-12-10, R.Mariotti(at)FinancialDataCorp.com (Bob Mariotti) <> wrote:
> On Tue, 09 Dec 2003 12:26:35 +0000, Gary Mayor <(E-Mail Removed)>
> wrote:
>
>>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";

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

Anno Siegel
Guest
Posts: n/a

 12-10-2003
Walt Mankowski <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On 2003-12-10, R.Mariotti(at)FinancialDataCorp.com (Bob Mariotti) <> wrote:

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

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