how to stop variable rounding up after a division

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mantrid

    #1

    how to stop variable rounding up after a division

    I am trying to take an integer and divide it by 100 to give a decimal to
    store in my database. The field in my mysql database is set to decimal(4,2)
    My php is
    $price=number_f ormat($price/100,2);
    Ive also tried
    $price=number_f ormat($price,2)/100;

    but neither work it keeps rounding any value less than 100 to 1 whereas I
    want it to store the value to two DP

    eg

    80 becomese 1 instead of 0.8
    180 becomes 2 instead of 1.8

    does anyone know how to do this?
    Ian


  • Colin Fine

    #2
    Re: how to stop variable rounding up after a division

    mantrid wrote:
    I am trying to take an integer and divide it by 100 to give a decimal to
    store in my database. The field in my mysql database is set to decimal(4,2)
    My php is
    $price=number_f ormat($price/100,2);
    Ive also tried
    $price=number_f ormat($price,2)/100;
    >
    but neither work it keeps rounding any value less than 100 to 1 whereas I
    want it to store the value to two DP
    >
    eg
    >
    80 becomese 1 instead of 0.8
    180 becomes 2 instead of 1.8
    >
    does anyone know how to do this?
    Ian
    >
    >
    AFAICS, number_format should deliver a string, which is what a
    decimal(4,2) expects.

    I wonder if you are failing to quote the string in your SQL? It would be
    easier to tell if you posted the code.

    On thing to try is to print out $price after the number_format, to check
    whether the rounding happens then or later.

    Colin

    Comment

    • Alvaro G. Vicario

      #3
      Re: how to stop variable rounding up after a division

      *** mantrid escribió/wrote (Sat, 07 Oct 2006 18:36:03 GMT):
      $price=number_f ormat($price/100,2);
      $price=number_f ormat($price,2)/100;
      80 becomese 1 instead of 0.8
      180 becomes 2 instead of 1.8
      In my system, this code:

      <?php

      echo number_format(8 0/100, 2) . "\n";
      echo number_format(8 0, 2)/100 . "\n";

      ?>

      prints this output:

      0.80
      0.8

      :-?


      You can try forcing a cast to float, that works in other languages:

      (float)$foo
      100.0


      --
      -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      ++ Mi sitio sobre programación web: http://bits.demogracia.com
      +- Mi web de humor con rayos UVA: http://www.demogracia.com
      --

      Comment

      • mantrid

        #4
        Re: how to stop variable rounding up after a division

        Yes that was it
        ,'$price', works in the sql
        whereas
        ,$price,
        does not
        thanks

        "Colin Fine" <news@kindness. demon.co.ukwrot e in message
        news:eg8too$agn $1$8300dec7@new s.demon.co.uk.. .
        mantrid wrote:
        I am trying to take an integer and divide it by 100 to give a decimal to
        store in my database. The field in my mysql database is set to
        decimal(4,2)
        My php is
        $price=number_f ormat($price/100,2);
        Ive also tried
        $price=number_f ormat($price,2)/100;

        but neither work it keeps rounding any value less than 100 to 1 whereas
        I
        want it to store the value to two DP

        eg

        80 becomese 1 instead of 0.8
        180 becomes 2 instead of 1.8

        does anyone know how to do this?
        Ian
        AFAICS, number_format should deliver a string, which is what a
        decimal(4,2) expects.
        >
        I wonder if you are failing to quote the string in your SQL? It would be
        easier to tell if you posted the code.
        >
        On thing to try is to print out $price after the number_format, to check
        whether the rounding happens then or later.
        >
        Colin

        Comment

        • Colin Fine

          #5
          Re: how to stop variable rounding up after a division

          Alvaro G. Vicario wrote:
          *** mantrid escribió/wrote (Sat, 07 Oct 2006 18:36:03 GMT):
          >$price=number_ format($price/100,2);
          >$price=number_ format($price,2 )/100;
          >
          >80 becomese 1 instead of 0.8
          >180 becomes 2 instead of 1.8
          >
          In my system, this code:
          >
          <?php
          >
          echo number_format(8 0/100, 2) . "\n";
          echo number_format(8 0, 2)/100 . "\n";
          >
          ?>
          >
          prints this output:
          >
          0.80
          0.8
          >
          :-?
          >
          >
          You can try forcing a cast to float, that works in other languages:
          >
          (float)$foo
          100.0
          >
          >
          That makes sense. The second example is not formatting the fraction, so
          it is printed in default format.

          Colin

          Comment

          Working...