Web Analytics Made Easy -
StatCounter Formatting number to one decimal place - CodingForum

Announcement

Collapse
No announcement yet.

Formatting number to one decimal place

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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>'
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      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.
      Be yourself. No one else is as qualified.

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎