Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Rounding up to 2 decimal places (http://www.velocityreviews.com/forums/t884095-rounding-up-to-2-decimal-places.html)

 Gary Mayor 12-09-2003 12:26 PM

Rounding up to 2 decimal places

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

 Anno Siegel 12-09-2003 12:49 PM

Re: Rounding up to 2 decimal places

Gary Mayor <gary@abertron.co.uk> 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 12-09-2003 12:51 PM

Re: Rounding up to 2 decimal places

Gary Mayor <gary@abertron.co.uk> 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 12-09-2003 12:58 PM

Re: Rounding up to 2 decimal places

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 <gary@abertron.co.uk> 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

 at 12-10-2003 01:36 AM

Re: Rounding up to 2 decimal places

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

 Walt Mankowski 12-10-2003 03:50 AM

Re: Rounding up to 2 decimal places

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

 Anno Siegel 12-10-2003 10:55 AM

Re: Rounding up to 2 decimal places

Walt Mankowski <waltman@pobox.com> 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

 All times are GMT. The time now is 11:54 PM.