Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
08-31-2011, 04:40 AM #1
- Join Date
- Aug 2002
- 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?
08-31-2011, 06:31 PM #2
- Join Date
- Jan 2011
- Thanked 656 Times in 655 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.
$num1 = '2.00';
$num2 = 2.00;
echo $num1, '<br>';
echo $num2, '<br>';
08-31-2011, 09:43 PM #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.
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.