0

I have a table in html. I want to round cell content to 2 decimal digits only.

<table>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tr>
                <td>Laptop</td>
                <td>2000.0000</td>
            </tr>
            <tr>
                <td>Mobile</td>
                <td>1000.0000</td>
            </tr>
        </table>

I have option to use CSS only. Is that possible in CSS?

Ravi Anand
  • 5,106
  • 9
  • 47
  • 77
  • 4
    Possible duplicate of [Formatting numbers (decimal places, thousands separators, etc) with CSS](https://stackoverflow.com/questions/8677805/formatting-numbers-decimal-places-thousands-separators-etc-with-css) – Marvin May 04 '18 at 17:12
  • Why do you have the option for only using CSS? You should be using Javascript for this. – chevybow May 04 '18 at 17:14
  • 1
    CSS is probably the worst way to approach this, but I'm going to assume that you have no access to the HTML – j08691 May 04 '18 at 17:15
  • HTML file is getting generated dynamically using powersheel. i would try to put external javascript file. – Ravi Anand May 04 '18 at 17:15
  • What does "i would try to put javascript external file" mean? – j08691 May 04 '18 at 17:16
  • 1
    If you're going to generate the HTML dynamically, do your fixed decimal point output when you generate the HTML – vogomatix May 04 '18 at 17:17
  • I will add javascript file along with css file too. but i was looking if something is available now to achieve same in CSS. – Ravi Anand May 04 '18 at 17:17

1 Answers1

-2

Using jQuery it would be something like this, assuming each cell containing a number had the number class:

$('tr.number').each( function () {
    // get value of table cell and convert to number...
    var val = parseFloat($(this).text());
    // put it back as fixed point value
    $(this).text(val.toFixed(2));
});
Martlark
  • 14,208
  • 13
  • 83
  • 99
vogomatix
  • 4,856
  • 2
  • 23
  • 46