SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: allowing fractions, but summing as float

  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    allowing fractions, but summing as float

    I need to add a field where someone can add a fraction, like 1/5 or 2/15. Later, those fields will be added up, and I'll need to give the sum total as a decimal.

    What's the correct way to make the field so I can add up the figures in PHP in decimal format? Should the field be of type varchar to accommodate the fraction, then I'll look for a PHP script on adding fractions and converting to decimal?

    Thanks,
    Steve

  2. #2
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,456
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    my suggestion is if you really require actual fractions to be accepted, that you at least validate and process them on input (regardless of whether the form field on the web page is a text box, dropdown, or whatever) and then store them as decimal values (not float)
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So they need to be decimal values in order to add them together? I was thinking that I should do something like:

    <option value="0.50">1/2</option>

    Thanks!
    Steve

  4. #4
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,456
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    yup, i mentioned dropdowns, that works
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,132
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, I'll keep it simple and go the drop-down route.

    Thanks!
    Steve

Bookmarks

Posting Permissions

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