help getting the remainder of a division in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jankie
    New Member
    • May 2007
    • 58

    help getting the remainder of a division in mysql

    hello everyone
    am trying to insert a division calculation into a mysql column but am unable to get mysql render the result together with the remainder.
    I understand i can get the remainder with the Mod or % operator,but that only returns the remainder alone
    i want 18/180 to return 0.1 or 0.10
    how can i do this
    currently it returns 0 ( no odd numbers ,just even ones.)
    thank you for your help
    [php]
    $period=180;//180 days
    $cost=18;//$18.00
    $query = "INSERT INTO mytable (id,cost,period ,cost_per_day) VALUES
    ('$id',$cost,$p eriod,(cost/period)";
    mysql_query($qu ery);
    [/php]
    cost_per_day field is decimal(integer makes no difference
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    What is the datatype of cost_per_day field ?

    Comment

    • Jankie
      New Member
      • May 2007
      • 58

      #3
      thank you debasisdas
      please dont bother,I fixed it
      for anyone having this same problem,here is how i figured it out.
      mysql5 defaults the decimal field to 10,0-decimal(10,0)
      0 means 0 digits on the right side.change it to 18,9(9 digits on each side-a setting that mimics what most calculators have)

      [php]
      ALTER TABLE `YourTable` CHANGE `Your_Decimal_C ol` `Your_Decimal_C ol` DECIMAL( 18, 9 ) NOT NULL;
      [/php]

      Comment

      Working...