# Rounding up to 2 decimal places

Discussion in 'Perl Misc' started by Gary Mayor, Dec 9, 2003.

1. ### Gary MayorGuest

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.

Any ideas

Gary

Gary Mayor, Dec 9, 2003

2. ### Anno SiegelGuest

Gary Mayor <> 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

Anno Siegel, Dec 9, 2003

3. ### Eric J. RoodeGuest

Gary Mayor <> 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

Eric J. Roode, Dec 9, 2003
4. ### Gary MayorGuest

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

>
>
> ceil( 100*\$x)/100
>
> Oh, and you could put question marks after your questions. You sound
> over-assertive without them
>
> Anno

Gary Mayor, Dec 9, 2003
5. ### atGuest

On Tue, 09 Dec 2003 12:26:35 +0000, Gary Mayor <>
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.
>
>
>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

at, Dec 10, 2003
6. ### Walt MankowskiGuest

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

Walt Mankowski, Dec 10, 2003
7. ### Anno SiegelGuest

Walt Mankowski <> 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

Anno Siegel, Dec 10, 2003