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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting number to one decimal place

    Ok, so I'm more of a designer and math was never my strong point.

    I'm keeping a record of points for a sort-of members database.

    Now most of the time, members are assigned whole numbers, 1 point, 2 points.

    Occasionally, they get quarter or half a point assigned (0.25, 0.5)

    So I set the points column in MySQL to DECIMAL(4,2)

    Throughout the website I now see 1.00, 2.00, 2.25

    What I'd like to see is 1, 2, 2.25

    In other words, truncated if the two decimal places are zeros.

    Can I solve this by changing the column type or do I have to do something when I query the data?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,771
    Thanks
    23
    Thanked 550 Times in 549 Posts
    It maybe how your getting your results into strings. See below. You can use use this example to change how your doing things. Just Hard to know for sure without code.
    PHP Code:
    $num1 '2.00';
    $num2 2.00;

    echo 
    $num1'<br>';
    echo 
    $num2'<br>'

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Not sure if this works in PHP, but it works in other languages/systems.

    Multiply the value you get from the DB by 1 before displaying it.

    That will make it lose its "string" quality and force it to become a PHP number. And then, presumably, the natural display of numbers via PHP will do what you want.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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