Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display values without decimals?

    Hi There,

    If I make a calculation for example:

    $a = 1;
    $b = 1.001;

    $answer = $a * $b;

    Can I possibly echo $answer without the .decimals?

    If so, please tell me how to do it!

    Thanks a lot,

    Gil

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm pretty sure you should add an (int) in front of the variable you want. Integers do not contain decimal points, so it simply truncates the decimals. For instance:

    PHP Code:
    $numbertotruncate = (int)$numbertotruncate
    -Obiwan Jabroni
    May the Schwartz be With You

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $answer = round($a*$b,0);

    the second parameter (the 0 ) is the decimel places returned.
    see also floor(number) rounds down
    and ceil(number) rounds up.

    note - you might need to do
    $answer = $a*$b; $answer = round($answer);
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #4
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot everyone,

    Both methods seem to work. With int I have to add,0. Thanks a lot.

    Like this:

    <?php

    $a = 1;
    $b = 2.4;
    $answer = round($a*$b,0);


    $d = 1;
    $e = 2.4;
    $answer2 = floor($d*$e);
    $answer3 = ceil($d*$e);
    $answer4 = (int)($d*$e);

    echo "<p>$a x $b = $answer (basic rounding 5+ 4-)";

    echo "<p>$d x $e = $answer2 (rounded down)";

    echo "<p>$d x $e = $answer3 (rounded up)";

    echo "<p>$d x $e = $answer4 (with int rounded down)";

    ?>

    Gil

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,074
    Thanks
    11
    Thanked 98 Times in 96 Posts
    or even ....

    PHP Code:
    <?
    $a
    =10;$b=3.46;
    echo 
    number_format($a $b,0); 
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow! More?

    Bye,

  • #7
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    More :
    PHP Code:
    <?
    $a 
    1.001;
    $b 1;
    $result explode(".",$a*$b);
    echo = 
    result[0];
    ?>
    I don't suffer from insanity, I enjoy every single minute of it!

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well if we're searching for different ways:
    PHP Code:
    <?php
    $a 
    1.001;
    $b 1;
    $result $a $b;
    echo 
    preg_replace('/^(\d+)\.\d+/''\1'$result);
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •