Web Analytics Made Easy -
StatCounter Display values without decimals? - CodingForum

Announcement

Collapse
No announcement yet.

Display values without decimals?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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

    Comment


    • #3
      $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.

      Comment


      • #4
        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

        Comment


        • #5
          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)

          Comment


          • #6
            Wow! More?

            Bye,

            Comment


            • #7
              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!

              Comment


              • #8
                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);
                ?>

                Comment

                Working...
                X