Rounding up to 2 decimal places

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

  1. Gary Mayor

    Gary Mayor Guest

    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
     
    Gary Mayor, Dec 9, 2003
    #1
    1. Advertising

  2. Gary Mayor

    Anno Siegel Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. Gary Mayor

    Gary Mayor Guest

    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.
    >
    >
    >>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
     
    Gary Mayor, Dec 9, 2003
    #4
  5. Gary Mayor

    at Guest

    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.
    >
    >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
     
    at, Dec 10, 2003
    #5
  6. 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.
    >>
    >>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
     
    Walt Mankowski, Dec 10, 2003
    #6
  7. Gary Mayor

    Anno Siegel Guest

    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
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eoin Mcloughlin

    Decimal places and huge fractions.

    Eoin Mcloughlin, Feb 11, 2004, in forum: Perl
    Replies:
    5
    Views:
    1,637
    Eoin Mcloughlin
    Feb 13, 2004
  2. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    354
    Curt_C [MVP]
    May 18, 2004
  3. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    561
    Curt_C [MVP]
    May 18, 2004
  4. Fuzzydave

    round not rounding to 0 places

    Fuzzydave, Aug 16, 2006, in forum: Python
    Replies:
    6
    Views:
    414
    Sybren Stuvel
    Aug 16, 2006
  5. Dr John Stockton

    Rounding a Number to a String with N decimal places

    Dr John Stockton, Aug 15, 2004, in forum: Javascript
    Replies:
    1
    Views:
    105
    G Roydor
    Aug 15, 2004
Loading...

Share This Page