-2

 <html>
    <head>
    </head>
    <body>
       <table border="1">
         <tr>
           <td>HTML Book Price</td>
           <td align="right">158.5</td>
         </tr>
         <tr>
           <td>CSS Book Price</td>
           <td align="right">125</td>
         </tr>
    </body>
</html>

i want to display book prices with two decimals. Ex: HTML Book Price 158.50 CSS Book Price 125.00..

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50
suresh
  • 23
  • 1
  • 7
  • Well, just edit the html markup how you like it to be. – arkascha Jul 19 '17 at 08:51
  • 1
    so...why can't you write "158.50" into the HTML then? – ADyson Jul 19 '17 at 08:51
  • @arkascha,@Adyson, thanks for your response, but that data is dynamically populating from database, we don't have the control database alters. – suresh Jul 19 '17 at 09:26
  • And you don't think that is something you should mention in the question? You have to take care of formatting in that dynamic layer, not in the final result, the html markup. So we cannot help here, since you did not post the stuff relevant here. – arkascha Jul 19 '17 at 09:32
  • Possible duplicate of [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – Alexander Elgin Jul 19 '17 at 10:14

2 Answers2

2

document.getElementById('html-book').innerHTML = (158.5).toFixed(2);
document.getElementById('css-book').innerHTML = parseFloat('125').toFixed(2);
<table border="1">
        <tr>
            <td>HTML Book Price</td>
            <td align="right" id="html-book">0</td>
        </tr>
         <tr>
            <td>CSS Book Price</td>
            <td align="right" id="css-book">0</td>
        </tr>
</table>
Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50
  • Hi Alexander, thanks for your response but data is populating dynamically from database. and in my case we don't have the control on database to change the type. – suresh Jul 19 '17 at 09:29
  • @suresh How do you generate the HTML (using JS, Java, PHP, Python or what)? – Alexander Elgin Jul 19 '17 at 09:57
  • I have generated HTML using JS – suresh Jul 19 '17 at 10:03
  • @suresh Actually your question had been answered already here: https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places. You should use `toFixed`. If the number comes to JS as a string you should parse it first using `parseFloat` – Alexander Elgin Jul 19 '17 at 10:21
0

If you are displaying price from database then store the price in double format in database or you can simply write two digits like @Alexander said.

Ravi Patil
  • 127
  • 2
  • 16