Putting a + or - in front of a number in a table

For discussions about game development that does not fit in any of the other topics.
Post Reply
Zyviel
Posts: 75
Joined: Tue Jun 08, 2010 8:12 pm

Putting a + or - in front of a number in a table

Post by Zyviel »

I am putting numbers into a html table to display the pluses or minuses on a players inventory item. The negative numbers show up with a minus but the positive numbers are just showing up with the number and no plus sign.

I'm just curious if there is an html or css formatting command that I can use to have a plus show up in front of positive numbers? If not I will add if statements in my php to check if the numbers are positive and then concatenate the + onto the numbers.

Thanks for any suggestions anyone has.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Putting a + or - in front of a number in a table

Post by Jackolantern »

Hmm...I have never actually dealt with this myself, but I wonder if <pre> may handle it? Often, <pre></pre> tags are used to format programming code because it tells the HTML rendering engine that you have preformatted the text and not to mess with it.

Maybe try that, and see what effect that has. It would keep unnecessary strain off of your server if you could create a client-side option to ensure the pluses show.
The indelible lord of tl;dr
Zyviel
Posts: 75
Joined: Tue Jun 08, 2010 8:12 pm

Re: Putting a + or - in front of a number in a table

Post by Zyviel »

Thanks for the reply Jackolantern.

I checked out the pre tag but it looks like I still have to put what I want to create inside the pre tags.

I can do that or I can just concatenate the + onto the number. Was just curious if there was a html or css tag that would make numbers show up with a plus automaticly. Thanks again for the suggestion.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Putting a + or - in front of a number in a table

Post by hallsofvallhalla »

hmm not sure on that one, i think a if statement may be the option

Code: Select all

if($variable > 0){$modifier = "+";}else {modifier = "";}

echo "Rocks=" . $modifier . $info3['rocks'];
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Putting a + or - in front of a number in a table

Post by Jackolantern »

I would honestly do it with Javascript, particularly if you are going to be having to process it on many different numbers in a table. I typically try to leave simple, presentation-oriented processing like that on the client-side, particularly if you are creating something that will demand Javascript be on, such as a game.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Putting a + or - in front of a number in a table

Post by Chris »

hallsofvallhalla wrote:hmm not sure on that one, i think a if statement may be the option

Code: Select all

if($variable > 0){$modifier = "+";}else {modifier = "";}

echo "Rocks=" . $modifier . $info3['rocks'];
A short hand if would be handier and quicker for this. No need for defining a variable.

Code: Select all

echo 'Rocks=' . $var < 0 ? '-' : '' . $info3['rocks'] 
But as Jack said you might want to look into JavaScript.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Putting a + or - in front of a number in a table

Post by hallsofvallhalla »

very pretty!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Putting a + or - in front of a number in a table

Post by Jackolantern »

Ok, I did a couple of tests on my own, and I don't quite understand the problem. Unless I am missing something, or did not do something right, when I did my tests, a simple "+" stayed in front of the numbers I put into table cells without any CSS, JS or additional tags. Is this maybe an issue with a certain browser, or did I maybe do something different?
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Putting a + or - in front of a number in a table

Post by Chris »

Reading over this topic again, I think you might be looking for the CSS content tag:

CSS

Code: Select all

.pos:before {
    content: '+';
}
HTML:

Code: Select all

<table>
    <tr>
        <th> Nº </th>
    </tr>
    <tr>
        <td class="pos">1</td>
    </tr>
    <tr>
        <td>-1</td>
    </tr>
    <tr>
        <td class="pos">69</td>
    </tr>
    <tr>
        <td class="pos">48</td>
    </tr>
    <tr>
        <td>-91</td>
    </tr>
</table>
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “General Development”