View Full Version : Formatting number to one decimal place

08-31-2011, 04:40 AM
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?

08-31-2011, 06:31 PM
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.

$num1 = '2.00';
$num2 = 2.00;

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

Old Pedant
08-31-2011, 09:43 PM
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.